# Win32ole --Test-- Ruby vs. Python

**URL:** <https://www.ruby-forum.com/t/win32ole-test-ruby-vs-python/47816>\
**Category:** Ruby\
**Created:** [November 14, 2005, 3:53am UTC](https://www.ruby-forum.com/t/win32ole-test-ruby-vs-python/47816 "2005-11-14T03:53:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghalsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d07c76/32.png) [@ghalsey](https://www.ruby-forum.com/u/ghalsey)\
**Post date:** [November 14, 2005, 3:53am UTC](https://www.ruby-forum.com/t/win32ole-test-ruby-vs-python/47816/1 "2005-11-14T03:53:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ghalsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d07c76/32.png) [@ghalsey](https://www.ruby-forum.com/u/ghalsey)\
**Post date:** [November 14, 2005, 3:59pm UTC](https://www.ruby-forum.com/t/win32ole-test-ruby-vs-python/47816/2 "2005-11-14T15:59:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ghalsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d07c76/32.png) [@ghalsey](https://www.ruby-forum.com/u/ghalsey)\
**Post date:** [November 14, 2005, 9:20pm UTC](https://www.ruby-forum.com/t/win32ole-test-ruby-vs-python/47816/3 "2005-11-14T21:20:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ghalsey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/d07c76/32.png) [@ghalsey](https://www.ruby-forum.com/u/ghalsey)\
**Post date:** [November 15, 2005, 12:32am UTC](https://www.ruby-forum.com/t/win32ole-test-ruby-vs-python/47816/4 "2005-11-15T00:32:45Z")

</div>

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
