I wrote a program in two languages. One in FreeBASIC (what else?) and one in Python.
Here is the program in FreeBASIC:
Code: Select all
Dim filename As String
Dim output_file As String
Dim input_file As String
Dim begin_ As Double
Dim end_ As Double
Dim pos1 As Integer
Dim pos2 As Integer
Dim in_path As String
Dim out_path As String
out_path = "d:\test_output\"
in_path = "c:\"
filename = Dir("c:\wikka.php@wakka*")
begin_ = Timer()
While len(filename) <> 0
pos1 = Instr(filename,"=") + 1
pos2 = Instr(filename,"%")
output_file = out_path + Mid(filename,pos1,pos2-pos1)
input_file = in_path + filename
filename = Dir()
end_ = Timer()
Print "Time spent = ";end_ - begin_
And here is the same program in Python (that is, functionally the same):
Code: Select all
output_path = 'd:\\wikka_files\\'
mylist = glob.glob('c:\\wikka.php@wakka*')
for el in range(len(mylist)):
f = open(mylist[el],'r')
str_ = f.read()
equalsign = mylist[el].split('=')
percent = equalsign.split('%')
new_name = percent
g = open(output_path+new_name,'w')
The program copies files from one directory to another while changing the name of the file. A very specific batch file copier/renamer, that is what it is.
Now for the benchmarks. Python comes with it's own benchmarking program. It's output when the above program runs:
Commandline: D:\Python25\python.exe D:\Python25\Lib\profile.py D:\fbpydoc\RENAME~1.PY
Timeout: 0 ms
24359 function calls (24356 primitive calls) in 6.073 CPU seconds
6 seconds to rename and copy 917 files (give or take a few).
And then the FreeBASIC benchmark.
On the first run it takes 6.4 seconds. On the second run it takes 6.7 seconds and after that it takes 5.12 seconds.
How the FreeBASIC benchmark should be interpreted.... I don't know. Perhaps some filecaching is going on making the program run faster on it's third try.
If I take the lowest number of the FreeBASIC benchmark (5.12 seconds) and the Python benchmark (6 seconds) then the difference is 1 second. That's less then 20% difference. Not a whole lot I think?
If someone can write a faster FreeBASIC version of the program (surely it can be done faster?) then please do post it.