i ran benchmarks , wondering why reversing string , comparing seems faster comparing individual characters.
reversing string worst case o(n) , comparison o(n), resulting in o(n), unless comparing same object, should o(1).
str = "test" str.reverse.object_id == str.object_id # => false
is character comparison worst case o(1)? missing?
edit
i extracted , simplified question here's code running.
def reverse_compare(str) str.reverse == str end def iterate_compare(str) # can test str[0] != str[-1] (str.length/2).times |i| return false if str[i] != str[-i-1] end end require "benchmark" n = 2000000 benchmark.bm |x| str = "cabbbbbba" # best case single comparison x.report("reverse_compare") { n.times reverse_compare(str) ; = "1"; end } x.report("iterate_compare") { n.times iterate_compare(str) ; = "1"; end } end user system total real reverse_compare 0.760000 0.000000 0.760000 ( 0.769463) iterate_compare 1.840000 0.010000 1.850000 ( 1.855031)
there 2 factors in favour of reverse method:
- both string#reverse , string#== written in pure c instead of ruby. inner loop uses fact length of string known, there no unnecessarry boundary checks.
- string#[] needs check string boundaries @ every call. main loop written in ruby thereby being tad bit slower well. create new (one character long) string object return needs handled, gcd, etc.
it looks these 2 factors have bigger performance gain better algorithm, done in ruby.
also note in test not testing random string, specific one, short well. if woud try larger one, possible ruby implementaion quicker.
Comments
Post a Comment