Win32ole --Test-- Ruby vs. Python

I am just starting to learn Ruby and I am a beginner at Python. So keep
that in mind with this simple test using SciTEVersion 1.59.

The task was to open up an Excel file, read down 1000 lines of data and
close the file.

I was suprised that Ruby 200% faster verses the 'static' generated
Python com object. The Python 'dynamic' was even slower so I won't
mention it's time. Psyco will not help this task either, in fact it
slows it down to 9.2 seconds.

Average of 3 runs- Python time:6.16
Average of 3 runs- Ruby time:2.89
Ruby was 200% faster-Chunky Bacon!
For this specific task, I am sorry to say "Python you're fired".

#The Python 2.4.1 Version:
import time
Gime = time.time()
from win32com.client import Dispatch

xlApp = Dispatch("Excel.Application")
xl_file = ('c:\\testdata.xls')
xlApp.Workbooks.Open(xl_file)
excel_cells_total=0

for r in range(1,1001):
    xlcell=xlApp.Cells(r,2).Value
    excel_cells_total=excel_cells_total+xlcell

xlApp.ActiveWorkbook.Close()
xlApp.Quit

print 'Excel 1000 cells total is '
print excel_cells_total
print 'Python Elapsed '
print (time.time() - Gime)

#Python Results:

python -c "import py_compile; py_compile.compile(r'C:\myruby\excel_data_2.py')"
Exit code: 0

pythonw -u "excel_data_2.py"

Excel 1000 cells total is
16132.9
Python Elapsed
6.26900005341

Exit code: 0
pythonw -u "excel_data_2.py"

Excel 1000 cells total is
16132.9
Python Elapsed
6.08899998665

Exit code: 0
pythonw -u "excel_data_2.py"

Excel 1000 cells total is
16132.9
Python Elapsed
6.11800003052

Exit code: 0

#The Ruby 1.8.2 Version:
time = Time.now

require 'win32ole'

xlApp=WIN32OLE::new('Excel.Application')
xl_file = ('c:\\testdata.xls')
xlApp.Workbooks.Open(xl_file)
excel_cells_total=0.0

for r in 1..1000
xlcell=xlApp.Cells(r,2).Value
excel_cells_total=excel_cells_total+xlcell
end

xlApp.ActiveWorkbook.Close()
xlApp.Quit

puts 'Excel 1000 cells total is ' + excel_cells_total.to_s
puts "Ruby Elapsed %f" % (Time.now - time)

ruby excel_data_2g.rb

Excel 1000 cells total is 16132.9
Ruby Elapsed 2.884000

Exit code: 0
ruby excel_data_2g.rb

Excel 1000 cells total is 16132.9
Ruby Elapsed 2.894000

Exit code: 0
ruby excel_data_2g.rb

Excel 1000 cells total is 16132.9
Ruby Elapsed 2.884000

···

Exit code: 0

greg.rb wrote:

I was suprised that Ruby 200% faster verses the 'static' generated
Python com object. The Python 'dynamic' was even slower so I won't
mention it's time. Psyco will not help this task either, in fact it
slows it down to 9.2 seconds.

Average of 3 runs- Python time:6.16
Average of 3 runs- Ruby time:2.89
Ruby was 200% faster-Chunky Bacon!
For this specific task, I am sorry to say "Python you're fired".

Wow - that's quite a surprising disparity. Also surprising is the fact you
mentioned in the old thread but not here, that the "static" generated Ruby
version (using olegen.rb) was slower than the straight dynamic WIN32OLE
code.

Might there be a relationship? Might Python's time difference also be in the
initial loading? What do you get if you loop and repeat from .Open to
..Close, say, 20 times? (The Ruby version takes about 20 times as long to do
that.)

And yet another surprise: Ruby out-performs the Windows Scripting Host, too,
with this test:

Excel 1000 cells total is 25 Apr 5894
VBScript Elapsed 3.359375

(The VBScript:)

t= Timer

Set xlApp = CreateObject("Excel.Application")
xl_file = "test.xls"
xlApp.Workbooks.Open xl_file
excel_cells_total = 0.0

For r = 1 To 1000
    xlcell = xlApp.Cells(r,1).Value
    excel_cells_total = excel_cells_total + xlcell
Next

xlApp.ActiveWorkbook.Close()
xlApp.Quit

WScript.Echo "Excel 1000 cells total is ", excel_cells_total
WScript.Echo "VBScript Elapsed ", (Timer - t)

Cheers,
Dave

Bonsoir ! Hi!

Regardez ce code : (look this coding : )

import time
Gime = time.time()
from win32com.client import Dispatch

xlApp = Dispatch("Excel.Application")
xl_file = ('c:\\testdata.xls')
xlApp.Workbooks.Open(xl_file)

excel_cells_total=0

#for i in xlApp.Range("B1:B1001").Value:
#1st mode
for i in xlApp.Range(xlApp.Cells(1,2),xlApp.Cells(1001,2)).Value: #2nd
mode
    excel_cells_total+=i[0]

xlApp.ActiveWorkbook.Close()
xlApp.Quit

print 'Excel 1000 cells total is '
print excel_cells_total
print 'Python Elapsed '
print (time.time() - Gime)

@-salutations

Michel Claveau

Thanks. It is a faster way to do it.

I was trying to keep the algorithms as = as possible to isolate the ole
aspect.

Using the improved method:

pythonw -u "someone.py"

Excel 1000 cells total is
51625817.0589
Python Elapsed
0.766000032425

Exit code: 0
ruby exceloletest2someone.rb

Excel 1000 cells total is 51625817.0589
Ruby Elapsed 0.515000

Exit code: 0

Now the numbers mis-represent the actual time. Ruby processing time is
faster but I think Ruby spends a second or two before the 'time' is
taken.
Python is the winner in this case.
-greg