On Wed, Nov 15, 2000, Chen Shapira wrote about "RE: [hackers-il] Summary of a Bash Lecture":
> BTW. Is there any knowledge about a theoretical limit of how well a language
> can be optimised "by design"?
> Meaning: a program written in c can be optimised for speed until it takes x
> seconds to run and no farther, while the same program in pascal can be
> optimised until it takes y seconds to run and no farther, where x!=y?
The "obvious" answer is that all languages can be optimized the same: if the
optimizer is smart enough, and given a program in C that can somehow be run
faster if written in Fortran, then the super-smart optimizer can convert the
program to Fortran as its first step.
But in real life, this is not so simple. Consider the following zsh script:
if [[ `date` = "never" ]]
for j in a b c
This can be optimized (if the optimizer is smart enough) to
if [[ `date` = "never" ]]
But if you realise that date can never return "never" (sounds like a James
Bond movie...), then it can be further optimized to
This looks like a contrived example, and it is, but it shows a point: a
smart enough optimizer can take code in Zsh, Perl, C++, or whatever, and
optimize it into the best possible assembly-language code (i.e., that no
other code that does the same thing runs faster). But how do you define
"doing the same thing"? Instead of running the "date" program, the shell
compiler/optimizer could call ctime() directly. But what if the installed
date program has some strange side-effects (e.g., changing the access time
of the file "/bin/date"!) that the program relies on? Maybe the whole point
of the above program was to be a "touch -a /bin/date" clone, and the "let i=.."
stuff, not the `date` stuff, needs to be optimized out?
The problem is that different languages let you do pretty high-level things
that each has dozens of implications and/or side-effects, so keeping all these
side-effects may mean we can't do any optimization at all...
As another example, consider a Tcl/Tk program (I hope you know what I'm
talking about). In Tcl/Tk, the callbacks of widgets are written in the
interpreted language Tcl; Because of this fact you (the programmer) can
change widget bindings on the fly, and other crazy stuff like that. And
all this interpreting is slow. It is obvious that it is possible to write
a different program in (say) C/Xlib, that does exactly the same thing, but
quicker, but making this transformation (also known as "optimization" - the
distinction between a "compiler" and "optimizer" in this context is a little
fuzzy) is nearly impossible in practice... Also, does this C/Xlib really does
the *same* thing? The Tcl/Tk program might be able to read widget bindings
from a user's script file, and the C/Xlib will not... So it's not exactly the
same thing - who decides if this optimization is a good enough approximation?
Nadav Har'El | Wednesday, Nov 15 2000, 17 Heshvan 5761
Phone: +972-53-245868, ICQ 13349191 |Willpower: The ability to eat only one