Re: Tcl faster than Perl/Python...but only with tricks...



Stephan Kuhagen wrote:
Earl Greida wrote:

"Stephan Kuhagen" <stk@xxxxxxxxxx> wrote in message
news:en5klt$7fa$1@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

On my computer (Linux 2.6.18, 2.6 GHz Pentium 4)..
0.273s for Perl
The Python-Version was....0.622s.
Tcl...0.937s

To me, the question is how often does the program need to read the 8.2MB
file. If every second then the time difference matters. If once a minute
then the few hundred milli-second difference becomes less important. If
only a few times then the time difference is essentially irrelevant. The
question becomes, which language is easier to use, and produces a program
that is easier to read, easier to maintain, easier to enhance, and more
reliable, over the life of the program.

This is quite right. The Perl-Version (for me) is not as readable as the Tcl
or the Python version. But Pythons "for line in file" is really simple and
good looking.

Slightly off-topic. If you like that Python "trick" then you'd love
tcllib's fileutil::foreachLine. If you don't have tcllib then copy and
paste this to your file:

# Slightly different name than tcllib but same semantics:
proc eachline {var fname script} {
upvar 1 $var v
set f [open $fname r]
while 1 {
set v [gets $f]
uplevel 1 $script
if {[eof $f]} break
}
close $f
}

Or if you like Python's version:

# With python semantics:
proc eachline {var args} {
upvar 1 $var v
if {[llength $args] == 3} {
if {[lindex $args 0] == "in"} {
set args [lrange $args 1 end]
} else {
error {invalid syntax}
}
} elseif {[llength $args] > 3} {
error {wrong # args: should be "eachline var ?in? filename
script.."}
}

set fname [lindex $args 0]
set script [lindex $args 1]

set f [open $fname r]
while 1 {
set v [gets $f]
uplevel 1 $script
if {[eof $f]} break
}
close $f
}

.


Quantcast