1

I was looking at what GCC does with the -O3(what I make sure that all of my code compiles with) and I was wondering how much usefullness of unswitching the loops is in an interpreted language. I was was wondering if it'd be worth it for code that is "production" ready, such as when you use google's closure compiler, since it'd speed it up a bit but not a lot I imagine. Well anyway how much benefit does this truly produce? I imagine that it is x checks for y loops. Where x is the number of if else ifs, or case switches, and y is the number of loops. So it's x*y. But how much is it worth it when you're using an interpreted language?

I'm asking it in a completely theoretical way, as I was just wondering about it, I doubt it's much of anything, and might even make it backwards since it has to read every character when it's running it.

OK here's what I'm talking about with the unswitching of the loops.

Also interpreted is what I mean by any language that is compiled at run-time and/or is ran through a VM, examples are; PHP, JavaScript, BASH, Ruby, Perl, etc.

I don't know if I worded it right above. But that's what I was meaning, in those languages wherein you have to read the input byte by byte and then transform it into something that you can use. I also know there are other things you can do to speed it up, but that's the simplest way that I can think of to explain it.

normal loop.

for(i=0;i<10;++i){
    if(something==3){
        do_something;
    }
    else{
        do_something_else;
}

unswitched loop(according to what I've been able to gather from the clang documentation(gcc's crap).

if(something=3){
 for(i=0;i<10;++i){
    do_something;
}
else{
 for(i=0;i<10;++i){
   do_something_else
 }
}

The code above is supposedly what they do.

133794m3r
  • 5,028
  • 3
  • 24
  • 37
  • "in an interpreted language" You mean, you are implementing a P-code interpreter by a switch-inside-a-loop construct? – wildplasser Jul 08 '12 at 22:51
  • As in like JavaScript, PHP, Ruby, BASH, etc. I thought that was "interpreted" as in the code is compiled at runtime and ran through the VM. And I know that it has to go through that effort everytime, and I was wondering if it's faster to unswitch the loop or just leave it as it is. Also I'll edit my question with some more info. – 133794m3r Jul 09 '12 at 00:52
  • I had to look up a p-code interpreter, but well it's what I said up above. I hope it makes it clearer to anyone who reads the question. – 133794m3r Jul 09 '12 at 01:00
  • gcc has nothing to do with "interpreted languages". Compiled languages, sure. – lornix Jul 10 '12 at 11:28
  • I never said it did, I was merely asking about the optimization technique. Which is what I said clearly in my question. I was using it as the example wherein I saw the thing stated. I've not yet read the dragon book, and thus had to go with what I had seen. I'm not some crazy person. Also GCC sort of does have to do with non compiled languages. As in GCJ which works with Java. – 133794m3r Jul 14 '12 at 23:26

0 Answers0