Integer vs string test time
-
I understand that
if vari=="0"is slower thenif vari==0. Is it better to doif vari.to_i==0? -
It seems to be slower to do the conversion then do the comparison. To test something like this you can use the Time.now() method to make a simple stopwatch. Here is how I tested it.
def str_test() t = Time.now() (1..10000000).each{|i| "0" == "0"} puts(Time.now() - t) end def int_test() t = Time.now() (1..10000000).each{|i| "0".to_i == 0} puts(Time.now() - t) endstr_test ran in 6.922 and int_test ran in 8.406, feel free to run the test yourself.
-
Thanks, I will have to remember how to do that:)
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement