最近遇到个诡异的问题。
本机 mac 10.9 ruby 2.2.0 Rails 4.2.0 costs = Benchmark.realtime {Time.now.at_beginning_of_month} *1000 => 0.0939369201660156
服务器 Ubuntu 12.04 ruby 2.2.0 Rails 4.2.0 costs = Benchmark.realtime {Time.now.at_beginning_of_month} *1000 1.925826072692871
两者时间相差太大,服务器上此方法耗时太长,而且代码中使用了大量这样的代码。影响性能。 各位大牛有没有什么思路。
注:各位机器上的耗时是多少啊?
#4 楼 @zwyan2009 可以把两台机器 cpu 的型号贴出来吗?
i3 CPU M 380 @ 2.53GHz
costs = Benchmark.realtime {Time.now.at_beginning_of_month} *1000
=> 0.234701
Benchmark.bm { |x| x.report {1000.times { Time.now.at_beginning_of_month } }}
这样写会不会好点
Ubuntu14.04 ruby2.0 rails4.0.2 2 Intel(R) Pentium(R) CPU G620 @ 2.60GHz
[1] pry(main)> costs = Benchmark.realtime {Time.now.at_beginning_of_month} *1000
=> 106.140972
[2] pry(main)> costs = Benchmark.realtime {Time.now.at_beginning_of_month} *1000
=> 0.101006
[3] pry(main)> costs = Benchmark.realtime {Time.now.at_beginning_of_month} *1000
=> 0.123881
[4] pry(main)> costs = Benchmark.realtime {Time.now.at_beginning_of_month} *1000
=> 0.085452
[5] pry(main)> costs = Benchmark.realtime {Time.now.at_beginning_of_month} *1000
=> 0.088573
[6] pry(main)> costs = Benchmark.realtime {Time.now.at_beginning_of_month} *1000
=> 0.0849
[7] pry(main)> costs = Benchmark.realtime {Time.now.at_beginning_of_month} *1000
=> 0.087018