Is there some difference between Time.now
and Time.new
(without parameters)? May be difference in memory management or some small details?
Asked
Active
Viewed 1,987 times
3

Paul
- 25,812
- 38
- 124
- 247
-
If you were aware that `now` was an alias for `new`, you should have said so in the question, as it would have helped us understand what you are after. I expect you knew that considering that the the doc for [Time.new](http://www.ruby-doc.org/core-2.1.4/Time.html#method-c-now) (v 2.1.4 anyway) consists of the single line, "Alias for ::new. Returns a Time object initialized to the current system time.". – Cary Swoveland Nov 03 '14 at 18:24
3 Answers
12
There is no difference.
Time.now is an alias for ::new. Returns a Time object initialized to the current system time.

Jeff Price
- 3,229
- 22
- 24
-
For me, it's not quite the same in all cases... `2.2.4 :010 > Timecop.freeze(Chronic.parse("February First")) => 2017-02-01 12:00:00 -0800` `2.2.4 :011 > Time.now => 2017-02-01 12:00:00 -0800` `2.2.4 :012 > Time.new => 2016-11-01 11:10:20 -0700` – nroose Nov 01 '16 at 18:12
-
1@nroose An old bug in Timecop? https://github.com/travisjeffery/timecop/issues/17 – Vanuan Oct 09 '19 at 00:56
7
now
is an alias for new
. There's no difference between them. Jeff price's get to answer(and his answer is also correct, please up vote his answer if you like this) first, because I was writing and running this benchmark:
Ruby 2.1.2(MRI):
Rehearsal ----------------------------------------------------------------------------
Time.new 0.670000 0.000000 0.670000 ( 0.679709)
Time.now 0.880000 0.010000 0.890000 ( 0.881899)
------------------------------------------------------------------- total: 1.560000sec
user system total real
Time.new 0.720000 0.000000 0.720000 ( 0.719453)
Time.now 0.740000 0.010000 0.750000 ( 0.742711)
Rehearsal ----------------------------------------------------------------------------
Time.new 0.810000 0.000000 0.810000 ( 0.811874)
Time.now 0.830000 0.000000 0.830000 ( 0.831346)
------------------------------------------------------------------- total: 1.640000sec
user system total real
Time.new 0.790000 0.010000 0.800000 ( 0.800082)
Time.now 0.740000 0.000000 0.740000 ( 0.749995)
Rehearsal ----------------------------------------------------------------------------
Time.new 0.680000 0.010000 0.690000 ( 0.690337)
Time.now 0.850000 0.000000 0.850000 ( 0.856800)
------------------------------------------------------------------- total: 1.540000sec
user system total real
Time.new 0.790000 0.010000 0.800000 ( 0.792666)
Time.now 0.770000 0.000000 0.770000 ( 0.777414)
Rehearsal ----------------------------------------------------------------------------
Time.new 0.590000 0.010000 0.600000 ( 0.594650)
Time.now 0.710000 0.010000 0.720000 ( 0.717067)
------------------------------------------------------------------- total: 1.320000sec
user system total real
Time.new 0.870000 0.000000 0.870000 ( 0.872646)
Time.now 0.680000 0.010000 0.690000 ( 0.687092)
Rehearsal ----------------------------------------------------------------------------
Time.new 0.780000 0.010000 0.790000 ( 0.786419)
Time.now 0.780000 0.000000 0.780000 ( 0.789049)
------------------------------------------------------------------- total: 1.570000sec
user system total real
Time.new 0.760000 0.010000 0.770000 ( 0.768194)
Time.now 0.790000 0.010000 0.800000 ( 0.790981)
Run benchmark yourself:
n = 1000000
5.times do
Benchmark.bmbm(40) do |x|
x.report("Time.new"){ n.times { Time.new } }
x.report("Time.now"){ n.times { Time.now } }
end
end

Surya
- 15,703
- 3
- 51
- 74
-
3Benchmarks will show no meaningful difference between `new` and `now` as `now` is an alias to `new`. Any differences are due to system processes or garbage collection and are basically random noise that occurred during the test. – the Tin Man Nov 03 '14 at 18:06
-
1@theTinMan : Actually, I ran benchmark for about 5 times per Ruby version before posting it here. If difference was due to system processes or garbage collection or because of any random noise that occurred during the test then results for time taken by `Time.new` shouldn't have been significantly lesser for all 5 runs. – Surya Nov 03 '14 at 18:23
-
I agree that these components do affect the benchmark, but shouldn't that be affecting it both the cases instead of being biased to `Time.new`? – Surya Nov 03 '14 at 18:31
-
1The only difference is that `now` calls `new`. That's a very, very, minor build-up and tear-down of the stack and subroutine call in C. "...significantly lesser..."? In 1,000,000 iterations you're only seeing a few hundredths/second difference, which in real life will be greatly offset by garbage collection, I/O and the OS subsystems. In other words, it's not worth worrying about and amounts to premature optimization. Also see the documentation for [`bmbm`](http://www.ruby-doc.org/stdlib-2.1.4/libdoc/benchmark/rdoc/Benchmark.html#method-c-bmbm). – the Tin Man Nov 03 '14 at 18:38
-
2@theTinMan : Ok, my bad, a bit lesser. I did some digging in time.c and I found out that `now` actually calls `rb_class_new_instance` while `new` `time_init_0`(in case where no argument is passed) which creates a current time and return immediately, while `rb_class_new_instance` goes upto object.c which ultimately allocate a new object of the class, i.e. it'll call `new`. So, yeah, it is an alias, but it has to go through other methods to reach the actual one. I agree that it's not such a worrying topic. By the way, thank you for your inputs. I always learn new things from people like you. :) – Surya Nov 03 '14 at 19:09
-
Flip the order of .now and .new and get back to us. In my tests, whichever ran second was faster. Didn't matter which. – Jeff Price Nov 03 '14 at 19:48
-
I did. Still not much change, for first run a bit more than `now` but in other 4 runs, `new` is again a bit lesser than `now`. You didn't have to down vote just because of that. I never said they are different, I just posted a benchmark on their performance, the "difference" word was used by theTinMan, I don't see how answer misleads anyone in any manner by the way. – Surya Nov 03 '14 at 20:04
1
Using Ruby 2.4.1 and Rails 5.0.3 When using travel_to in tests Time.new does not get affected by it but Time.now does change because of it

Natan Rubinstein
- 665
- 1
- 9
- 27
-
1And this is sad :(. But I think we have to avoid using `Time.new` in the meaning of `Time.now`. In that case we shoot out two targets:). Readability and problems with `travel_to`. – Евгений Масленков May 28 '19 at 08:09