English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I want to control a boolean value in a function, should I pass the variable as "true" or "false", or integer like 1 or 0 ??? Which one is efficient ?

function testBoolean($var=true){
if($var){
.....
}
}

function testInteger($var=1){
if($var == 1){
......
}
}

$testBoolean(false);
$testInteger(0);

So, which function works faster?

2006-07-20 01:24:01 · 6 answers · asked by mdy 2 in Computers & Internet Programming & Design

6 answers

i wouldn't think so but the testInteger is faster.
testBoolean Loaded in 7.08103179932 seconds
testInteger Loaded in 3.48091125488 seconds
-----------------------CODE-------
function microtime_float(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function testBoolean($var=true){
if($var){
return true;
}
}

function testInteger($var=1){
if($var == 1){
return true;
}
}
$time_start = microtime_float();
testBoolean(false);
$time_end = microtime_float();
$time = $time_end - $time_start;
echo "Loaded in $time seconds
";

$time_start = microtime_float();
testInteger(0);
$time_end = microtime_float();
$time = $time_end - $time_start;
echo "Loaded in $time seconds
";
?>

2006-07-20 02:50:53 · answer #1 · answered by oc_surf 2 · 0 0

I don't know that there will be an aprecialble difference in overall performance, but a boolean value is one bit, an integer is 4 bits. so for memory a boolean value is 1/4 the size of a regular integer.

2006-07-20 01:53:41 · answer #2 · answered by John J 6 · 0 0

I'd imagine that the difference is negligible. In a compiled language it wouldn't make a difference, because they would both be handled as bytes internally anyways. PHP may be different because of the interpretation process, but like I said, I doubt there's enough of a difference to actually worry about. Because of this I'd go with "true" and "false" - it makes the code far more readable than zeros and ones.

Rawlyn.

2006-07-20 01:35:48 · answer #3 · answered by Anonymous · 0 0

I don't know much about php, but in other programming languages the boolean value has less overhead. The integer requires more memory.

2006-07-20 01:29:02 · answer #4 · answered by Anonymous · 0 0

definitely the boolean. cos the purpose of boolean is use for testing, hence it is built to give a fast performance and output.

2006-07-20 05:57:05 · answer #5 · answered by Anonymous · 0 0

If there is a speed difference, it would be in microseconds.

2006-07-20 09:48:59 · answer #6 · answered by Anonymous · 0 0

fedest.com, questions and answers