Home > Archive > Tcl > June 2007 > Is there already a "sourcecode to optimized converter" for tcl?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Is there already a "sourcecode to optimized converter" for tcl?
|
|
| Michael Reichenbach 2007-06-30, 4:18 am |
| I wanted to optimize my scripts and already tested already
tclpro/activestate tcl compiler but got really disappointed, the
improvement was more then minimal. The bytecode hides the source but
this is not my point, I wanted to speed up things.
I can imagine that this (=good readable for me)
proc testproc { name args } {
puts $name
puts $args
# ....
}
proc nextproc { name args } {
puts $name
puts $args
# ....
}
is slower then this (="optimized")
proc testproc {n args} {puts $n;puts $args};proc nextproc {n args} {puts
$n;puts $args}
Is there already a project to convert the source in such a style?
(everything in one line, removing comments, removing wasted spaces,
shorter variablenames, ...many more)
| |
| Aric Bills 2007-06-30, 4:18 am |
| There is a bytecode compiler built into the Tcl interpreter. Anything
inside a proc gets bytecode compiled the first time that proc is
called. The ActiveState Tcl compiler "remembers" the compiled form of
your code, which means you save a miniscule amount of time when you
first run the program (because the compilation is already done), but
once the code is bytecode-compiled, it will run at the same speed
whether the file you sourced was compiled or not. (ActiveState makes
no bones about the fact the goal of their compiler is not to improve
performance but to protect intellectual property.)
So your code is already getting optimized--the bytecode compiler will
take care of any issues that arise due to comments or white space. If
your bytecode-compiled code is not fast enough for you, here are some
steps to take:
* identify places where you're not taking advantage of optimizations
in Tcl. For example, have you put your expressions inside curly
braces (e.g. [expr {$myvar * 2}] rather than [expr $myvar * 2])?
http://wiki.tcl.tk/348 is a good starting place to learn about
performance tweaks.
* identify bottlenecks in your code and optimize those algorithms. If
you can boil down those bottlenecks to minimal examples, I'm sure
readers of c.l.t. can help you maximize their efficiency.
* if things are still too slow for you, consider rewriting the slow
parts in C as a Tcl extension.
Throughout this process, the [time] command is your friend. Whenever
possible, make sure to time several iterations of the code in question
(e.g., [time {myproc} 1000]) to minimize the effect of unusually slow
iterations. At a minimum, call the proc once before timing it
(otherwise you end up timing bytecode compilation AND code execution,
which will seriously confound your results).
Hope that helps,
Aric
| |
| Jeff Hobbs 2007-06-30, 4:18 am |
| Michael Reichenbach wrote:
> I wanted to optimize my scripts and already tested already
> tclpro/activestate tcl compiler but got really disappointed, the
> improvement was more then minimal. The bytecode hides the source but
> this is not my point, I wanted to speed up things.
As Arcis already noted, the Tcl Dev Kit Compiler is a precompiler for
obfusacation purposes - it is not intended for to improve performance,
and it doesn't advertise itself that way (although I understand that
"Compiler" means different things to different people).
> I can imagine that this (=good readable for me)
...
> is slower then this (="optimized")
>
> proc testproc {n args} {puts $n;puts $args};proc nextproc {n args} {puts
> $n;puts $args}
That is not correct. One byte-compiled, the whitespace is irrelevant.
Arcis already provided several pointers, but one page to look at is
http://wiki.tcl.tk/1611. It's a benchmark page, but you'll notice that
some of the benchmarks are improved through alternate coding methods for
the same purpose.
Jeff
|
|
|
|
|