Last active
August 29, 2015 14:01
-
-
Save dacamp/5f0743f166500b928b85 to your computer and use it in GitHub Desktop.
Ruby hex vs to_i(16) vs sprintf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'benchmark' | |
Benchmark.bm do |x| | |
x.report( ".hex 10^4 ") { 10000.times { "ff".hex } } | |
x.report( ".to_i(16): 10^4 ") { 10000.times { "ff".to_i(16) }} | |
x.report( "sprintf: 10^4 ") { 10000.times { sprintf "%i\n" % "0xff" }} | |
x.report( ".hex: 10^5 ") { 100000.times { "ff".hex } } | |
x.report( ".to_i(16): 10^5 ") { 100000.times { "ff".to_i(16) }} | |
x.report( "sprintf: 10^5 ") { 100000.times { sprintf "%i\n" % "0xff" }} | |
x.report( ".hex: 10^6 ") { 1000000.times { "ff".hex } } | |
x.report( ".to_i(16): 10^6 ") { 1000000.times { "ff".to_i(16) }} | |
x.report( "sprintf: 10^6 ") { 1000000.times { sprintf "%i\n" % "0xff" }} | |
x.report( ".hex: 10^7 ") { 10000000.times { "ff".hex } } | |
x.report( ".to_i(16): 10^7 ") { 10000000.times { "ff".to_i(16) }} | |
x.report( "sprintf: 10^7 ") { 10000000.times { sprintf "%i\n" % "0xff" }} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user system total real | |
.hex 10^4 0.010000 0.000000 0.010000 ( 0.003354) | |
.to_i(16): 10^4 0.000000 0.000000 0.000000 ( 0.003762) | |
sprintf: 10^4 0.020000 0.000000 0.020000 ( 0.022379) | |
.hex: 10^5 0.030000 0.000000 0.030000 ( 0.028870) | |
.to_i(16): 10^5 0.030000 0.000000 0.030000 ( 0.031195) | |
sprintf: 10^5 0.210000 0.000000 0.210000 ( 0.212064) | |
.hex: 10^6 0.300000 0.000000 0.300000 ( 0.291441) | |
.to_i(16): 10^6 0.320000 0.000000 0.320000 ( 0.324304) | |
sprintf: 10^6 2.200000 0.010000 2.210000 ( 2.202667) | |
.hex: 10^7 3.210000 0.000000 3.210000 ( 3.327383) | |
.to_i(16): 10^7 4.640000 0.020000 4.660000 ( 4.887118) | |
sprintf: 10^7 29.820000 0.090000 29.910000 ( 31.304471) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user system total real | |
.hex 10^4 0.000000 0.000000 0.000000 ( 0.003637) | |
.to_i(16): 10^4 0.000000 0.000000 0.000000 ( 0.003048) | |
sprintf: 10^4 0.030000 0.000000 0.030000 ( 0.023090) | |
.hex: 10^5 0.030000 0.000000 0.030000 ( 0.029000) | |
.to_i(16): 10^5 0.030000 0.000000 0.030000 ( 0.029577) | |
sprintf: 10^5 0.240000 0.010000 0.250000 ( 0.241405) | |
.hex: 10^6 0.260000 0.000000 0.260000 ( 0.265409) | |
.to_i(16): 10^6 0.290000 0.000000 0.290000 ( 0.291048) | |
sprintf: 10^6 2.180000 0.000000 2.180000 ( 2.177367) | |
.hex: 10^7 2.730000 0.000000 2.730000 ( 2.734876) | |
.to_i(16): 10^7 2.970000 0.000000 2.970000 ( 2.979812) | |
sprintf: 10^7 26.560000 0.090000 26.650000 ( 29.873252) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user system total real | |
.hex 10^4 0.000000 0.000000 0.000000 ( 0.003550) | |
.to_i(16): 10^4 0.010000 0.000000 0.010000 ( 0.003121) | |
sprintf: 10^4 0.020000 0.000000 0.020000 ( 0.021431) | |
.hex: 10^5 0.030000 0.000000 0.030000 ( 0.029200) | |
.to_i(16): 10^5 0.030000 0.000000 0.030000 ( 0.030162) | |
sprintf: 10^5 0.210000 0.000000 0.210000 ( 0.209630) | |
.hex: 10^6 0.290000 0.000000 0.290000 ( 0.292072) | |
.to_i(16): 10^6 0.310000 0.010000 0.320000 ( 0.310538) | |
sprintf: 10^6 2.070000 0.000000 2.070000 ( 2.077301) | |
.hex: 10^7 2.790000 0.000000 2.790000 ( 2.789065) | |
.to_i(16): 10^7 3.170000 0.010000 3.180000 ( 3.181919) | |
sprintf: 10^7 22.890000 0.060000 22.950000 ( 23.067154) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user system total real | |
.hex 10^4 0.000000 0.000000 0.000000 ( 0.004351) | |
.to_i(16): 10^4 0.010000 0.000000 0.010000 ( 0.003111) | |
sprintf: 10^4 0.020000 0.000000 0.020000 ( 0.021106) | |
.hex: 10^5 0.030000 0.000000 0.030000 ( 0.027120) | |
.to_i(16): 10^5 0.020000 0.000000 0.020000 ( 0.028414) | |
sprintf: 10^5 0.210000 0.000000 0.210000 ( 0.202262) | |
.hex: 10^6 0.250000 0.000000 0.250000 ( 0.249064) | |
.to_i(16): 10^6 0.300000 0.000000 0.300000 ( 0.309073) | |
sprintf: 10^6 2.010000 0.000000 2.010000 ( 2.005965) | |
.hex: 10^7 2.570000 0.000000 2.570000 ( 2.573061) | |
.to_i(16): 10^7 2.930000 0.000000 2.930000 ( 2.939949) | |
sprintf: 10^7 21.090000 0.040000 21.130000 ( 21.170493) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment