Bijan
Bijan

Reputation: 8586

Perl If/Else Optimization

Suppose I have a string $a = "abc";

In terms of optimization and speed, would it be faster if I ran

if ($a ne "abc") {
    print "Not abc";
} else {
    print "abc";
}

Versus

if ($a eq "abc") {
    print "abc";
} else {
    print "Not abc";
}

? I am asking because I have a loop that will typically enter the else statement and I wanted to know if I could make it run more efficiently by switching the if/else clauses

Upvotes: 0

Views: 330

Answers (1)

Kenny Grage
Kenny Grage

Reputation: 1124

If you are typically going to go to the else block in the first example, then yes, the second example would be a little bit faster. However, when I say a little bit, I mean a very small amount of optimization that you probably won't notice unless you are running the code millions of times.

Upvotes: 1

Related Questions