Getting the time of the received message

2019-03-22 02:27发布

问题:

How does one get the time of the received message in Erlang?

I want to calculate something according to the frequency of the received messages to the gen_server.

e.g. message 1, some time, message 2 some time. get the time between messages.

Thanks

回答1:

You can use statistics(wall_clock) each time you receive a message. The second member of the tuple it returns will be the time between the two receives (in milliseconds).

Edit:

As rvirding mentions in his comment, you can also use now() and then calculate the time difference accordingly. Take a look at supervisor.erl found in the $ERL_TOP/lib/stdlib/src/ directory of your Erlang/OTP distribution. The last lines of that module (functions addRestart, inPeriod and difference) calculate the frequency of restarts using now().



标签: erlang