diff --git a/README.md b/README.md index 163b6fbc..4d6a59ec 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,8 @@ Int64         Returns the current utc epoch timestamp in second ```ruby -.now_utc_to_unix_ms : -Int64 +.now_utc_to_unix_ms_f : +Float64 ```         Returns the current utc epoch timestamp in msecond @@ -42,17 +42,17 @@ Float64 ### Benchmark : ```shell $> crystal -v -Crystal 0.27.2 [60760a546] (2019-02-05) +Crystal 0.34.0 [4401e90f0] (2020-04-06) -LLVM: 4.0.0 +LLVM: 8.0.0 Default target: x86_64-unknown-linux-gnu $> cd bench $> crystal build --release bench.cr $> ./bench - Time.utc_now.to_unix 19.49M ( 51.31ns) (± 1.25%) 0 B/op 1.13× slower - Crtimestamp.now_utc_to_unix 21.98M ( 45.49ns) (± 0.72%) 0 B/op fastest - Time.utc_now.to_unix_ms 19.59M ( 51.03ns) (± 0.93%) 0 B/op 1.12× slower -Crtimestamp.now_utc_to_unix_ms 21.86M ( 45.74ns) (± 0.98%) 0 B/op fastest - Time.utc_now.to_unix_f 19.47M ( 51.36ns) (± 1.57%) 0 B/op 1.12× slower - Crtimestamp.now_utc_to_unix_f 21.88M ( 45.71ns) (± 0.95%) 0 B/op fastest + Time.utc.to_unix 31.17M ( 32.08ns) (± 0.79%) 0.0B/op 1.20× slower + Crtimestamp.now_utc_to_unix 37.30M ( 26.81ns) (± 0.15%) 0.0B/op fastest + Time.utc.to_unix_ms 27.54M ( 36.31ns) (± 0.53%) 0.0B/op 1.36× slower +Crtimestamp.now_utc_to_unix_ms 37.36M ( 26.77ns) (± 0.09%) 0.0B/op fastest + Time.utc.to_unix_f 29.93M ( 33.41ns) (± 4.18%) 0.0B/op 1.25× slower + Crtimestamp.now_utc_to_unix_f 37.33M ( 26.79ns) (± 2.33%) 0.0B/op fastest ``` diff --git a/bench/bench.cr b/bench/bench.cr index fd523cb7..ccaa454d 100644 --- a/bench/bench.cr +++ b/bench/bench.cr @@ -2,14 +2,14 @@ require "benchmark" require "../src/crtimestamp" Benchmark.ips do |x| - x.report("Time.utc_now.to_unix") { Time.utc_now.to_unix } + x.report("Time.utc.to_unix") { Time.utc.to_unix } x.report("Crtimestamp.now_utc_to_unix") { Crtimestamp.now_utc_to_unix } end Benchmark.ips do |x| - x.report("Time.utc_now.to_unix_ms") { Time.utc_now.to_unix_ms } - x.report("Crtimestamp.now_utc_to_unix_ms") { Crtimestamp.now_utc_to_unix_ms } + x.report("Time.utc.to_unix_ms") { Time.utc.to_unix_ms } + x.report("Crtimestamp.now_utc_to_unix_ms") { Crtimestamp.now_utc_to_unix_ms_f } end Benchmark.ips do |x| - x.report("Time.utc_now.to_unix_f") { Time.utc_now.to_unix_f } + x.report("Time.utc.to_unix_f") { Time.utc.to_unix_f } x.report("Crtimestamp.now_utc_to_unix_f") { Crtimestamp.now_utc_to_unix_f } end diff --git a/shard.yml b/shard.yml index 8fe8fe8f..9614246c 100644 --- a/shard.yml +++ b/shard.yml @@ -1,12 +1,12 @@ name: crtimestamp -version: 0.1.0 +version: 0.2.0 -crystal: 0.27.2 +crystal: 0.34.0 authors: - - Nicolas Dudniczenko + - ndudnicz description: | - Faster way to get a timestamp than calling standard Time + Faster way to get a timestamp than calling standard Time lib license: GPL-3.0 diff --git a/src/crtimestamp.cr b/src/crtimestamp.cr index 424422a5..5a6ecd8d 100644 --- a/src/crtimestamp.cr +++ b/src/crtimestamp.cr @@ -31,7 +31,7 @@ struct Crtimestamp # ``` # Time.now_utc_to_unix_ms # => 1452567845876 # ``` - def self.now_utc_to_unix_ms : Int64 + def self.now_utc_to_unix_ms_f : Float64 seconds, nanoseconds = Crystal::System::Time.compute_utc_seconds_and_nanoseconds (seconds * 1_000 + (nanoseconds / NANOSECONDS_PER_MILLISECOND)) - EPOCH_MSECONDS_TIMESTAMP end