Re: Tcl faster than Perl/Python...but only with tricks...
- From: Stephan Kuhagen <stk@xxxxxxxxxx>
- Date: Sat, 30 Dec 2006 16:41:37 +0100
Hello
I have tried some small variations by mimicking your first python code
(as I don't know enough perl to understand the perl code
he, he, me too, but somehow it did the thing... ;-)
). For me,
the tcl code that does similar to the python code is:
proc do {} {
set f [open bigfile r]
foreach { s } [split [read $f] \n] {
if { [regexp -nocase {destroy} $s] } {
puts $s
}
}
}
do
I have enclosed the code in a proc, and I am treating the file as a
list, as python does :-)
The "for x in file" in Python give only list semantics, but it reads the
file line by line. I also tried a solution in a proc with the whole file
used as a list. This was a little bit faster, but not as fast as Perl, and
it also suffers from the fact, that it would not work for really big files.
The above code is faster than python. Perl is of course unbeatable and
the second python code uses a special feature of python (filters on
files?), which to me is equivalent to the trick you also did with
"regexp -all". So, my ranking is: :-)
Yes, the filter is a neat trick, but also has the advantage not to read the
whole file into memory.
It would be interesting to have a version of python that reads line by---
line (like gets in tcl) to compare (I don't know python, so perhaps I am
asking something silly?).
def do():
f=file('bigfile')
r = re.compile(r'destroy', re.IGNORECASE)
line=f.readline()
while line!="":
if r.search(line):
print line.rstrip("\r\n")
line=f.readline()
do()
---
Surprising for me, this is indeed slower than the Tcl version of gets/while
in a proc:
0.822s (Python)
0.659s (Tcl)
OTOH, nobody would write something like this in Python, since in the
tutorial shows you to use for...in or filter() (unfair trick! ;-) as soon
as you start learning Python. In Tcl I thinks it is natural to use
while/gets and not some weird read/regexp-combination.
But aside from that, I wondering, if while and gets (or one of them) have
such a big performance impact, and why. Remember, even if I remove the if,
regex and puts in the loop, it just gets twice as fast. I would expect,
that if, regex and puts together should use much more time than while and
gets.
Regards
Stephan
.
- Follow-Ups:
- Re: Tcl faster than Perl/Python...but only with tricks...
- From: Cameron Laird
- Re: Tcl faster than Perl/Python...but only with tricks...
- From: Alexandre Ferrieux
- Re: Tcl faster than Perl/Python...but only with tricks...
- References:
- Tcl faster than Perl/Python...but only with tricks...
- From: Stephan Kuhagen
- Re: Tcl faster than Perl/Python...but only with tricks...
- From: George Petasis
- Tcl faster than Perl/Python...but only with tricks...
- Prev by Date: Re: Who uses peer text widgets?
- Next by Date: Need example of ::cmdline::getfiles usage for newby
- Previous by thread: Re: Tcl faster than Perl/Python...but only with tricks...
- Next by thread: Re: Tcl faster than Perl/Python...but only with tricks...
- Index(es):
Relevant Pages
|