0

In general, is code length affective to the speed of the program? I'm talking about differences in small sizes not comparing 10,000 lines to 10.

Example #1:

// Ternary Operator
$todo = (empty($_POST['todo'])) ? 'default' : $_POST['todo'];

VS

// The above is identical to this if/else statement
if (empty($_POST['todo'])) {
    $action = 'default';
} else {
    $action = $_POST['todo'];
}

And other examples like not using common brackets and universal indentation.

Example #2:

if ($gollum === 'halfling') {
    $height --;
}

VS

if ($gollum === 'halfling') $height --;

Also using CamelCase naming convention will lower the characters length, is that effective at all? I doubt this is effective, but still it uses less characters.

Cheers!

3
  • for small size , no such difference. Commented Aug 23, 2014 at 6:34
  • Yes, but applying this to the whole program will make a difference. Commented Aug 23, 2014 at 6:39
  • 2
    Just for anyone curious, the ternary operator is slower. Consider this benchmark: codepad.org/fmXcTDEs Commented Aug 23, 2014 at 6:59

2 Answers 2

5

In your first example, you are using two different constructs, namely the 'ternary operator' and an 'if / else' statement.

Since the 'if / else' statement is more general (ie you can do more thing with it), the compiler / parser will have more difficulties to optimize it. So I would say that the ternary operator might be a little bit faster.

In the second example, it is only about coding style, both codes will probably be compiled / parsed to exactly the same thing, so no gain here.

However, premature optimization is the root of all evil ! The gain from doing such micro tiny optimization, even on a whole application will probably be negligible and will most of the time be at the expense of readability and maintainability. So I would strongly advice against that kind of thing.

Concerning characters length, it won't matter at all, the compiler / parser will transform that anyway to some other representation that could be understood by the computer, so the only thing that will change is the space that your source code will take on the hard drive.

Just write your code the way it is easier for you to understand and let the compiler / parser do those kind of thing. It is much better to optimize the algorithm itself than the way of writing it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your words, "It is much better to optimize the algorithm itself than the way of writing it." +1
1

I think you shoud concern coding style more than efficiency now.

if you already have a style guide, follow it. if you not, you can find one or create one. There aren't such difference between your codes both of two examples. You can pick anyone to create your own style guide.

2 Comments

I'm following WordPress coding standards make.wordpress.org/core/handbook/coding-standards/php even if I'm not coding for WordPress, I like it because it's easier to read. But it uses more characters.
I think wordpress uses the 2nd version of your example 1, and the 1st version of your exmpale 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.