Created
September 14, 2012 23:37
-
-
Save gnarg/3725615 to your computer and use it in GitHub Desktop.
jruby solution to histogram coding challenge
This file contains hidden or 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
| require 'thread' | |
| class Chunk | |
| def initialize(file, size, offset) | |
| @handle = File.open(file) | |
| @handle.pos = offset | |
| @string = @handle.read(size) | |
| if @string.index("\n") < @string.index(',') | |
| look_back(4) | |
| end | |
| end | |
| def each_age | |
| offset = 0 | |
| while pos = @string.index(',', offset) | |
| if eol = @string.index("\n", pos) | |
| yield @string[pos + 1..eol - 1] | |
| offset = eol + 1 | |
| else | |
| offset = pos + 1 | |
| end | |
| end | |
| end | |
| def update_results(results) | |
| offset = 0 | |
| while pos = @string.index(',', offset) | |
| if eol = @string.index("\n", pos) | |
| age = @string[pos + 1..eol - 1] | |
| results[age] += 1 | |
| offset = eol + 1 | |
| else | |
| offset = pos + 1 | |
| end | |
| end | |
| end | |
| def look_back(bytes) | |
| @handle.seek(- @size - bytes, IO::SEEK_CUR) | |
| @string = @handle.read(bytes) + @string | |
| end | |
| end | |
| THREAD_COUNT = 10 | |
| chunk_size = File.size(ARGV[0]) / THREAD_COUNT | |
| threads = [] | |
| results = [] | |
| THREAD_COUNT.times do |i| | |
| results[i] = Hash.new.tap{|h| h.default = 0 } | |
| threads << Thread.new do | |
| chunk = Chunk.new(ARGV[0], chunk_size, i * chunk_size) | |
| # this slows it down to 19s for some infernal reason | |
| # chunk.each_age do |age| | |
| # results[i][age] += 1 | |
| # end | |
| chunk.update_results(results[i]) | |
| end | |
| end | |
| threads.each{|t| t.join} | |
| histogram = results.inject{|memo,hash| memo.merge(hash) {|_,a,b| a + b}} | |
| p histogram |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment