-3

It appears that (!$a == 'hello') is consistently faster than ($a != 'hello')

// (!$a == 'hello')
Used time: 52.743232011795
Used time: 52.633831977844
Used time: 51.452646970749

//($a != 'hello')
Used time: 76.290767908096
Used time: 81.887389183044
Used time: 64.569777011871

Any idea why this is happening? I understand that this level of optimization is irrelavent in most of the cases. The question is purely out of curiosity. (Ref: http://www.php.net/manual/en/language.operators.comparison.php#99216)

mixdev
  • 2,724
  • 2
  • 30
  • 25

1 Answers1

5

!$a == 'hello' casts $a to a (negated) boolean and compares that to a string. That may be faster, since it's easier to decide than actually comparing two strings. It'll also give you wrong results. What you need to compare against is !($a == 'hello'), which I would guess is about equal in time taken.

deceze
  • 510,633
  • 85
  • 743
  • 889