Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Conversation

@samsonasik
Copy link
Contributor

@adamlundrigan
Copy link
Contributor

Is there a definitive benchmark on this?

Knowing nothing about properly benchmarking language features, I wrote this:

<?php

for ( $i = 0; $i < 10; $i++ )
{

    $array = range(0,5000);
    $start = microtime(true);
    var_dump(empty($array));
    var_dump((microtime(true) - $start) * 1000000);
    unset($array, $start);
    
    $array = range(0,5000);
    $start = microtime(true);
    var_dump(count($array) === 0);
    var_dump((microtime(true) - $start) * 1000000);
    unset($array, $start);
    
    $array = range(0,5000);
    $start = microtime(true);
    var_dump($array == []);
    var_dump((microtime(true) - $start) * 1000000);
    unset($array, $start);
    
    echo "\n\n";

}

https://3v4l.org/GpIT6

Sample result

bool(false)
float(55.074691772461)
bool(false)
float(1256.9427490234)
bool(false)
float(19.073486328125)

bool(false)
float(19.073486328125)
bool(false)
float(13.113021850586)
bool(false)
float(13.113021850586)

bool(false)
float(11.920928955078)
bool(false)
float(21.934509277344)
bool(false)
float(12.874603271484)

bool(false)
float(12.15934753418)
bool(false)
float(16.927719116211)
bool(false)
float(13.113021850586)

bool(false)
float(11.205673217773)
bool(false)
float(11.920928955078)
bool(false)
float(11.920928955078)

bool(false)
float(11.920928955078)
bool(false)
float(12.15934753418)
bool(false)
float(15.020370483398)

bool(false)
float(20.027160644531)
bool(false)
float(13.113021850586)
bool(false)
float(13.113021850586)

bool(false)
float(17.881393432617)
bool(false)
float(14.066696166992)
bool(false)
float(12.15934753418)

bool(false)
float(20.027160644531)
bool(false)
float(13.828277587891)
bool(false)
float(12.874603271484)

bool(false)
float(11.920928955078)
bool(false)
float(12.15934753418)
bool(false)
float(14.066696166992)

but my results aren't consistent. After the first iteration the discrepancy is small with neither method being consistently "ahead".

@samsonasik
Copy link
Contributor Author

the benchmark code can be found at http://maettig.com/code/php/php-performance-benchmarks.phps

@Xerkus Xerkus added this to the 2.7.0 milestone Oct 13, 2017
@Xerkus Xerkus closed this in b007590 Oct 13, 2017
Xerkus added a commit that referenced this pull request Oct 13, 2017
@samsonasik samsonasik deleted the count-improve branch March 2, 2018 03:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants