Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace inappropriate uses of Time.now #7155

Merged
merged 4 commits into from
Dec 10, 2018

Conversation

straight-shoota
Copy link
Member

For measuring elapsed time, Time.measure should be used. In applications, where a time zone won't be used anyway, it's better to use Time.utc_now instead of Time.now to avoid loading a time zone which can be expensive (loading from disk) when it is completely unnecessary.

The time value is converted to Unix time anyway, so there is no point in assigning a time zone.
It will be transmitted as UTC time anyway, so there is no point in assigning a time zone.
Copy link
Member

@asterite asterite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having to put everything inside a block hurts readability. Isn't there a similar way to do it without a block?

@straight-shoota
Copy link
Member Author

@asterite Sure, the method body of #measure could just be inlined directly:

start = Time.monotonic
do_stuff
elapsed = Time.monotonic - start

This would be closer to the previous usage of Time.now but with a proper clock.

I'd prefer to use .measure, but .monotonic is also possible.

@ysbaddaden
Copy link
Contributor

@asterite really?

res = yield
elapsed = Time.measure do
print "#{name}: "
res = nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is not needed.

This is why I don't like the block form of measure, it forces you to adjust your code (variables inside the block will not be seen outside it).

@asterite
Copy link
Member

asterite commented Dec 7, 2018

@ysbaddaden Yes. Because it's a block it forces you to declare some variables outside of it, as in my comment above. The other form is less obtrusive.

@ysbaddaden
Copy link
Contributor

OK, I understand.

@@ -384,24 +384,26 @@ class RedBlackTreeRunner
end

def bench(name, n = 1)
t = Time.now
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes to this file are actually already done in #6454, samples/red_black_tree.cr.

@RX14 RX14 merged commit c9e92ce into crystal-lang:master Dec 10, 2018
@RX14 RX14 added this to the 0.27.1 milestone Dec 10, 2018
@straight-shoota straight-shoota deleted the jm/fix/time-now branch December 10, 2018 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants