Hacker News new | ask | show | jobs
by swerling 2981 days ago
The ruby version looked a bit odd to me. It would be unusual to not use regex in this context.

I'm not sure if this is in the spirit of the test, but using regex reduced the time from 27s to 0.04s on my machine.

    require 'benchmark'

    contents = File.read("/tmp/200mb.txt")
    find = "07123E1F482356C415F684407A3B8723E10B2CBBC0B8FCD6282C49D37C9C1ABC"
    result = nil
    elapsed = Benchmark.realtime do
      result = (contents =~ /#{find}/)
    end
    puts "Found at index #{result} in #{elapsed}s"

(The search string was placed at the end of a 200mb file)
1 comments

You can reduce the time to 0.02s by just using `String#index`.