From: ptkwt@... (Phil Tomson) Date: 2005-06-17T08:50:33+09:00 Subject: Re: Super-scalar Optimizations In article <1118962660.509611.111140@f14g2000cwb.googlegroups.com>, Phrogz wrote: >I was looking over the shoulder of a C++ coworker yesterday, when he >was writing a hack to only run certain code once. The C++ code was the >equivalent of the following Ruby code: > > already_run = false > while true > foo = bar if !already_run > already_run = true > do_something( ) > end > >I asked him: "Wouldn't it be slightly faster to nest the boolean >assignment inside the if statement?" I was suggesting the equivalent >of: > > already_run = false > while true > if !already_run > foo = bar > already_run = true > end > do_something( ) > end > >His answer was "no", and involved discussion of super-scalar >architectures and the fact that the original way ran the assignment in >parallel to the condition evaluation, and so was in fact faster. > > >The reason I'm posting is - are there any such considerations in Ruby >(when writing Ruby code, not C/C++ components)? > >Or am I correct in assuming that the current state of >compilation/interpretation is such that there is no parallel branching >of statements to worry about? > I'll take a stab at this, but I'm no expert.... Your cow-worker _may_ be right about the differences between the two code snippets. I would think that you'd have to know what kind of assembly code got generated by the two different snippets, though. Perhaps he's taken a look at the compiled code. It also seems like it could differ a lot between compilers (g++ vs. VC++). As far as Ruby code goes, I don't think you would see any difference because Ruby doesn't get compiled to native code (yet ;-). Though there may be differences in how the interpretter handles the two different snippets which could possibly effect speed, it would have nothing to do with anything deep down in the actual hardware processor. Phil