Difference between revisions of "Talk:While"
Jump to navigation
Jump to search
(Created page with "How is it that a do loop is faster than a while loop? I wrote some code to test this and they look equal to me.~~~~") |
|||
Line 1: | Line 1: | ||
How is it that a do loop is faster than a while loop? I wrote some code to test this and they look equal to me.[[User:Lowrez Resident|Lowrez Resident]] 14:58, 7 August 2013 (PDT) | How is it that a do loop is faster than a while loop? I wrote some code to test this and they look equal to me.[[User:Lowrez Resident|Lowrez Resident]] 14:58, 7 August 2013 (PDT) | ||
:The while loop in the LSO compiler produces the following output for the various loop types. That is unless they decided to do a little optimizing (they were NEVER going to optimize LSO). | |||
<pre> | |||
//while loop | |||
test: | |||
if(condition)false jump end; //6 bytes (1 for bytecode, 1 for type designation for conditional, 4 bytes for jump offset). | |||
//loop body goes here | |||
jump test;//5 bytes (1 for bytecode, 4 bytes for jump offset). | |||
end: | |||
//do-while loop | |||
start: | |||
//loop body goes here | |||
if(condition)true jump start;//6 bytes (1 for bytecode, 1 for type designation for conditional, 4 bytes for jump offset). | |||
</pre> | |||
:As you can see, for each loop iteration the while loop executes two bytecodes while the do-while executes only one. Of course LL could have fixed this by rewriting the while as follows: | |||
<pre> | |||
jump test;//5 bytes (1 for bytecode, 4 bytes for jump offset). | |||
start: | |||
//loop body goes here | |||
test: | |||
if(condition)true jump start;//6 bytes (1 for bytecode, 1 for type designation for conditional, 4 bytes for jump offset). | |||
</pre> | |||
:--'''[[User:Strife_Onizuka|Strife]]''' <sup><small>([[User talk:Strife_Onizuka|talk]]|[[Special:Contributions/Strife_Onizuka|contribs]])</small></sup> 18:10, 7 August 2013 (PDT) |
Latest revision as of 17:10, 7 August 2013
How is it that a do loop is faster than a while loop? I wrote some code to test this and they look equal to me.Lowrez Resident 14:58, 7 August 2013 (PDT)
- The while loop in the LSO compiler produces the following output for the various loop types. That is unless they decided to do a little optimizing (they were NEVER going to optimize LSO).
//while loop test: if(condition)false jump end; //6 bytes (1 for bytecode, 1 for type designation for conditional, 4 bytes for jump offset). //loop body goes here jump test;//5 bytes (1 for bytecode, 4 bytes for jump offset). end: //do-while loop start: //loop body goes here if(condition)true jump start;//6 bytes (1 for bytecode, 1 for type designation for conditional, 4 bytes for jump offset).
- As you can see, for each loop iteration the while loop executes two bytecodes while the do-while executes only one. Of course LL could have fixed this by rewriting the while as follows:
jump test;//5 bytes (1 for bytecode, 4 bytes for jump offset). start: //loop body goes here test: if(condition)true jump start;//6 bytes (1 for bytecode, 1 for type designation for conditional, 4 bytes for jump offset).