views:

222

answers:

6

I've always been curious about this. Between the two code example which is more processor efficient? I understand that various languages may be different and I need to know what the answer is in PHP, but I am also curious about javaScript, CFScript, and ActionScript if the answer would be different.

Thanks

ps:The example may not be exact for a particular language, but I'm sure you will get the point

Example 1:

if(myVar < 1){
  return;//returned with nothing
else{
  //do something
}

Example 2:

if(myVar < 1){
  //left blank
else{
  //do something
}

EDIT: Guys, you're right, this would probably be completely unnecessary, I am asking this out of curiosity more than anything. I've been doing web development for more than a decade and It's all pretty much been self taught. I've seen a lot of code using both methods from "trained professionals" and was wondering if this was a personal preference or one group of people knew something the other didn't.

Also these are pseudo code examples.I'm asking about multiple languages where the details would be slightly different and the test is a simple one just to make sure you knew what I was asking about. It should be assumed that these examples would be in a function.

+2  A: 

As long as there are no statements after the end of the if..else block, I believe they are identical in most languages.

That being said, I tend to favor using as few return statements as possible as I find it makes my code more readable and easier to debug. I doubt it's worth your time to concern yourself with efficiency in this case - focus on keeping your code as clean as possible.

pix0r
+9  A: 

If you're looking for a performance problem in your code, this is not it. If you haven't benchmarked your performance, then you don't have a performance problem (yet).

Use the method that best expresses your intentions. Anything else is baseless micro-optimisation.

Update: Assume that the people writing the compiler are smarter than you are (this isn't always true, but it works most of the time). The compiler will look at the code you've written, and figure out the best way to represent that in object code (whether that is machine code or bytecode or some other kind of intermediate representation). So if you've got two chunks of code that mean exactly the same thing, chances are good that the compiler will generate the same output in both cases.

Greg Hewgill
While you are most certainly correct, you're not answering the actual question.
zombat
A: 

Depends on what is happening and what happens most often in that part of the code to get the most efficency from it.

Hazior
A: 

I would suggest using "return null;" but not for speed reasons, simply because you can be sure the function stops running when you expect it too. For example, it's possible you or somebody else will modify this function, add code outside the if statement and not realize the code shouldn't have run under certain conditions.

It's generally a good idea to force things to happen a way you expect, don't let it work by coincidence. Mind you I'm getting a bit more theory than practicality, but it's always better safe than sorry.

TravisO
A: 

In the first example, you don't need the else.

They should compile equivalently, if the compiler is decent.

Sometimes a deeply nested set of ifs can be converted to an unnested sequence of ifs, or ifs connected with ANDs can be converted into a straight sequnce of ifs each of which does a return in the positive case. This can improve readability.

if (a and b and c and d and e and f)
    do_something()

can be converted to:

if (!a)
   return;

if (!b)
   return;

if (!c)
   return;

if (!d)
   return;

if (!e)
   return;

if (!f)
   return;

do_something();

In my trivial example, the first way is probably easier to read, but when a-f are each fairly complicated expressions, the 2nd way can be easier to read, result in less indentation, etc.

smcameron
+1  A: 

Leaving the return out is faster.

Test This

Setup

function a(){if(true) return; else ;}

Code Under Test

a();

Tear Down

Output


Ran in 0.042321920394897 seconds 0.00018978118896484 over


And This

Setup

function b(){if(true) ; else ;}

Code Under Test

b();

Tear Down

Output


Ran in 0.042132139205933 seconds


Tested for 100,000 repetitions.

Don
testing on from chrome console, a() is about 10 nanoseconds faster than b() for me.
Jimmy
@Jimmy, that Javascript, eh? interesting.
Don