python - Reasons for this disparity in execution speed? -


I wrote a quick python script to compare two files, each of which contained an uncashed hash, it verified To do that, both the files are similar to the order, then I write it again in Ruby for educational purposes.

The Python implementation takes seconds, while the implementation of Ruby takes up to four minutes.

I have thought that this is most likely for me lack of Ruby knowledge, any thoughts on what I am doing?

Environment

: \\ file1.txt ',' r ') for hash = dict () f.readlines () in line: if not in the hash row: hash [line] = 1 other: hash [line] + = 1 print " F.close () f = open ('c: \\ file2.txt', 'r') for the row in the full file 1 "f.readose (): if the swan is not in line: print" hash not found! " Has [=]: hash [key]! = 0: Print "Uneven hash count:% s": For the key in the hash, "file2" num_errors = 0 hash for print [line] - = 1 f.close () Print. % Key num_errors + = 1 print "% d match overall"% num_errors

ruby ​​

  file = file. Open ("c: \\ file1.txt", "r") hash = {} file.each_line {| Line | Ifhes.has_key? File (row) hash [line] + = 1 and hash [line] = 1 end} file.close () "file 1 file" file parsing file.open ("c: \\ file2.txt", "r") File.each_line {| Line | If hashes.has_key? (Line) hash [line] - = 1 and puts "Not found hash!" End} file.close () returns "integer file 2" num_errors = 0 hashes.each_key {| Key | If Hesh [Key]! = 0 num_errors + = 1 end} "Total # {num_errors} found matches found"  

To give ideas for scale , edit each file 900 000 HES is bigger than

PROGRESS

Using Nandavada's suggestions, here is the customized Ruby script:

  f1 = "c : \\ file1.txt "f2 =" c: \\ file2.txt "hash = hash.new (0) file. Open (F1, "R"). F | While line = f.gets hash [line] + = end of end no_fonds = 0 file. Open (F2, "R"). F | Whereas the line = f.gets hash is the hash (line) hash [line] - = 1 other node_founders + = 1 end end end num_errors = hashes.values.to_a.select {| Z | Z! = 0} .size enters "total # {not_founds} rows in file2" puts "not found" # "{num_errors} matching totals"  

Windows 1.8 with Ruby 7, the original version took 250 seconds and took customized version 223.

On a Linux VM! Running Rabbi 1.9.1, the original version went into 81 seconds, about 1/3 of the windows 1.8.7 times as of Interestingly, the customized version takes 98 seconds. Note that while the line = ... memory was necessary due to constraints.

On windows with Ruby 1.9.1, the original took 457 seconds and took customized version 543.

On Windows with JRuby, took original 45 seconds and took customized version 43.

I am surprised by some extent results, I was expecting 1.9.1 to improve over 1.8.7.

I refer to Ruby's reference implementation (well, Ruby) (non-scientifically ) The dog has slowed down.

If you have an opportunity, please try to run your program under JRB! Charles Nutter and other folks claim to ruby ​​dramatically.

I am most interested in your results.


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -