Quote:
Originariamente inviato da VICIUS
Non riesco a prendere sonno così mi sono messo a fare qualche modifica. La nuova versione è circa il 39% più veloce.
...
Molto semplicemente ho sostituito il ciclo che cercava se i valori erano contenuti nelle estrazioni con un confronto tra due integer.
|
Vicius ehm...
so che ti sto scassando i cabasisi ma mi da errore. Puoi azzizzarmelo per la versione 1.8.7?
Questo è il codice(non mi cambiare i percorsi dei file):
Codice:
def benchmark(label)
print "#{label}..."
start_time = Time.now
result = yield
puts " completato. (#{(Time.now - start_time).to_s} secondi)"
result
end
def compress(values)
c = 0
values.each do |x|
c |= 1 << x
end
c
end
def load_data(file_name)
data = Array.new
File.open(file_name, 'r') do |file|
file.gets; file.gets # ignora le prime due righe
while line = file.gets
date, wheel, values = line.chop.split
values = values.split(',').map { |x| x.to_i }
data << { date: date, wheel: wheel, values: values, c: compress(values) }
end
end
data
end
def load_find(file_name)
data = Array.new
File.open(file_name, 'r') do |file|
file.gets # ignora la prima riga
while line = file.gets
values = line.chop.split(',').map { |x| x.to_i }
data << { values: values, c: compress(values) }
end
end
data
end
def search(data, find)
find.each do |r|
puts "-- #{r[:values].inspect} --"
data.each do |e|
puts "#{e[:date]} #{e[:wheel]} #{e[:values].inspect}" if (e[:c] & r[:c]) == r[:c]
end
end
end
#data = benchmark('Caricamento delle estrazioni') { load_data('C:\Scaricamenti\Temp\Gugo\Contest 07 - Il Lotto\LottoPiccolo\LOTTO.D1') }
#find = benchmark('Caricamento delle ricerche') { load_find('C:\Scaricamenti\Temp\Gugo\Contest 07 - Il Lotto\LottoPiccolo\LOTTO.D2') }
data = benchmark('Caricamento delle estrazioni') { load_data('C:\Scaricamenti\Temp\Gugo\Contest 07 - Il Lotto\LottoGrosso\LOTTO.D1') }
find = benchmark('Caricamento delle ricerche') { load_find('C:\Scaricamenti\Temp\Gugo\Contest 07 - Il Lotto\LottoGrosso\LOTTO.D2') }
benchmark('Ricerca dei valori') { search(data, find) }