sketchucation logo sketchucation
    • Login
    ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

    Integer vs string test time

    Scheduled Pinned Locked Moved Developers' Forum
    3 Posts 2 Posters 196 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • honoluludesktopH Offline
      honoluludesktop
      last edited by

      I understand that if vari=="0" is slower then if vari==0. Is it better to do if vari.to_i==0?

      1 Reply Last reply Reply Quote 0
      • C Offline
        Cleverbeans
        last edited by

        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)
        end
        
        

        str_test ran in 6.922 and int_test ran in 8.406, feel free to run the test yourself.

        1 Reply Last reply Reply Quote 0
        • honoluludesktopH Offline
          honoluludesktop
          last edited by

          Thanks, I will have to remember how to do that:)

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Buy SketchPlus
          Buy SUbD
          Buy WrapR
          Buy eBook
          Buy Modelur
          Buy Vertex Tools
          Buy SketchCuisine
          Buy FormFonts

          Advertisement