Last active
December 22, 2015 04:58
-
-
Save fukayatsu/6420247 to your computer and use it in GitHub Desktop.
某クロッシング問題をredisで解いてみた: ( https://codeiq.jp/ace/yuki_hiroshi/q432 )
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
#!/usr/env/ruby -v | |
require 'benchmark' | |
measure = Benchmark.measure { | |
require 'redis' | |
redis = Redis.new | |
redis.del :ray | |
INPUT = 'crossing.txt' | |
rays = [] | |
open(INPUT).read.each_line { |line| | |
rays << line.chomp.to_i - 1 | |
} | |
p rays.count | |
puts | |
crossing_count = 0 | |
rays.each.with_index do |ray, i| | |
redis.zadd :ray, ray, ray | |
crossing_count += redis.zcount(:ray, ray + 1, '+inf') | |
puts "#{i}, #{Time.now}" if i % 10000 == 0 | |
end | |
puts | |
p crossing_count | |
} | |
puts Benchmark::CAPTION | |
puts measure #=> 45秒ぐらい |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment