在Erlang 18中, 我们还是可以使用erlang:now/0

但也已经 deprecated啦,大量使用可能引发瓶颈。

The default time warp mode has the same behavior as before, and the old API still works. Thus, you are not required to change anything unless you want to. However, you are strongly encouraged to use the new API instead of the old API based onerlang:now/0. erlang:now/0 is deprecated, as it is and will be a scalability bottleneck.

By using the new API, you automatically get scalability and performance improvements. This also enables you to use the multi-time warp mode that improves accuracy and precision of time measurements.

具体问题分析可以看看余峰2013年写的。总的就是,有锁,大规模使用可能会引起POSIX time jumping,进而可能不准确。

1. 原来我们取系统时间用的是 erlang:now/0, 在新版API中我们使用erlang:system_time/0代替

1> erlang:system_time().
1456761315297998397

它返回的是当前的 Erlang system time in native time unit. 如果你还是需要原来的erlang:now/1的三元组方式,可以使用

2> erlang:timestamp().
{1456,761486,888869}

那么我们怎么衡量上面说的time unit(时间最小单元)使用convert_time_unit/3把1s转为最小单元是纳秒

3> erlang:convert_time_unit(1, seconds, native).
1000000000

再看

4> Fun = fun() -> Begin = erlang:monotonic_time(), timer:sleep(5000), End = erlang:monotonic_time(), End - Begin end.
#Fun<erl_eval.20.54118792>
5> Fun().
5005813139

得到的结果,根本不是5000000000整。多运行几次Fun().会发现每次的结果都不一样。

2.以前我们经常使用erlang:now/0来规定事件的先后顺序,因为它是单调递增的。

现在我们有了新的API.

6> erlang:unique_integer([monotonic]).
-576460752303423487

这个函数可以帮组我们产生单调数字,但是仅限于同一个runtime system instance。 如果多节点的分布式系统中,就无法保证单调。

如果你想记录事件发生时的顺序和时间。那么可以使用一个二元tuple

7> {erlang:monotonic_time(), erlang:unique_integer([monotonic])}.
{-576458615893212445, -576460752303423487}

如果还要加上系统时间就可以再加上

8> {erlang:monotonic_time(), erlang:unique_integer([monotonic]), erlang:system_time()}.
{-576457144539468688,-576460752303423488,1456762913149935455}

3.如果你只是想产生一个唯一的标示。那么可以使用

9> erlang:unique_integer().
-576460752303423452

或者使用正数的

10> erlang:unique_integer([positive]).
45

4.生成随机数

以前使用erlang:now/0和random.seed生成随机数。现在可以使用

11> random:seed(erlang:monotonic_time(), erlang:unique_integer(), erlang:monotonic_time()).
undefined
12> random:uniform().
0.03920785389270476

5.总结.

不要再使用erlang:now/0啦!

如果你想兼容以前的旧代码,那么快快来看这个官方的例子是怎么做到的:)

https://github.com/erlang/otp/blob/maint/erts/example/time_compat.erl

官方关于time的文档:

http://erlang.org/doc/apps/erts/time_correction.html


When my code crashes all the time, but restarts without anybody knowing

[Erlang35]Erlang18的time的更多相关文章

  1. [Erlang18]教练!又发现Erlang Shell里面的神奇函数一只

      人嘛,总是想提高效率,创造更多的价值,同时也得到更多的选择空间.可一个人的精力,时间终归是有限的,减少自身重复或无意义工作就显得格外重要! 要么懂得授权,要么把重复的工作交给机器来做: 现实: 美 ...

  2. RabbitMQ、Rdis

    RabbitMQ RabbitMQ:官方网址:http://www.rabbitmq.com/ Install Erlang # rpm -ivh erlang-18.3-1.el6.x86_64.r ...

  3. rabbitmq+ keepalived+haproxy高可用集群详细命令

    公司要用rabbitmq研究了两周,特把 rabbitmq 高可用的研究成果备下 后续会更新封装的类库 安装erlang wget http://www.gelou.me/yum/erlang-18. ...

  4. rabbitMQ安装及部署

    1.安装 rpm -ivh erlang-18.3-1.el6.x86_64.rpm, 下载地址:http://www.rabbitmq.com/releases/erlang/ rpm --impo ...

  5. CentOS7安装RabbitMQ集群

    实验环境 RabbitMQ 集群 server1.example.com    IP: 10.10.10.11    Node: diskserver2.example.com    IP: 10.1 ...

  6. [2016-03-15]rabbitmq notes

    安装 系统:CentOS 6 环境: rabbitmq 依赖的Erlang环境包 wget https://www.rabbitmq.com/releases/erlang/erlang-18.1-1 ...

  7. RabbitMQ高可用集群配置

    1.安装RabbitMQ 1)下载和安装erlang 下载erlang wget http://www.rabbitmq.com/releases/erlang/erlang-18.1-1.el6.x ...

  8. centos安装rabbitmq

    RabbitMQ是流行的开源消息队列系统,是AMQP(Advanced Message Queuing Protocol高级消息队列协议)的标准实现,用erlang语言开发.RabbitMQ据说具有良 ...

  9. rabbitMQ linux安装

    1,下载 erlang (http://www.rabbitmq.com/releases/erlang/) wget http://www.rabbitmq.com/releases/erlang/ ...

随机推荐

  1. linux下今天遇到的问题

    之前由于测试需要,必须用mysql5.7的客户端, 现在由于产品完善,开始支持5.6,所以需要装5.6的客户端做测试,考虑到手工测试的效率及不可重复性,准备自动化执行原来的用例. 老的用例是用MySQ ...

  2. postman-2get发送请求

    文档地址:https://www.v2ex.com/p/7v9TEc53 第一个API请求 最热主题 相当于首页右侧的 10 大每天的内容. https://www.v2ex.com/api/topi ...

  3. Ubuntu 升级内核版本

    查看当前内核版本 sch01ar@ubuntu:~$ uname -r Ubuntu 内核地址:https://kernel.ubuntu.com/~kernel-ppa/mainline/ 打开这个 ...

  4. pandas read_csv读取大文件的Memory error问题

    今天在读取一个超大csv文件的时候,遇到困难:首先使用office打不开然后在python中使用基本的pandas.read_csv打开文件时:MemoryError 最后查阅read_csv文档发现 ...

  5. leetcode475

    public class Solution { public int FindRadius(int[] houses, int[] heaters) { houses = houses.Distinc ...

  6. TBitConverter

    TBitConverter FromBcd  FromByte  FromCurrency  FromDouble  FromExtended  FromInteger  FromLargeInt   ...

  7. LUA中相同签名函数覆盖

    function test() print('test')end function test() print('test22222')end test() --输出test2222

  8. How To Install Spring IDE In Eclipse

    Spring IDE is a very useful graphical user interface tool adding support for Spring Framework. In th ...

  9. Node.js中的express框架获取http参数

    最近本人在学习开发NodeJs,使用到express框架,对于网上的学习资料甚少,因此本人会经常在开发中做一些总结. express获取参数有三种方法:官网介绍如下 Checks route para ...

  10. Python divmod() 函数

    Python divmod() 函数  Python 内置函数 python divmod() 函数把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b). 在 pyt ...