Skip to content

Instantly share code, notes, and snippets.

@loganlinn
Created March 22, 2012 23:00
Show Gist options
  • Save loganlinn/2165297 to your computer and use it in GitHub Desktop.
Save loganlinn/2165297 to your computer and use it in GitHub Desktop.
PHP strcmp+strtolower VS strcasecmp Benchmark
for i in 50 100 250 500; do echo "String length: $i"; php strcmpbench.php $i; php strcasecmpbench.php $i; echo "\n"; done
# Example run:
#
# String length: 50
# ************************
# * strcomp + strtolower *
# ************************
# Time: 0.000082
# Memory: 1456
# ************************
# * strcasecmp *
# ************************
# Time: 0.000057
# Memory: 1488
#
#
# String length: 100
# ************************
# * strcomp + strtolower *
# ************************
# Time: 0.000165
# Memory: 1552
# ************************
# * strcasecmp *
# ************************
# Time: 0.000111
# Memory: 1552
#
#
# String length: 250
# ************************
# * strcomp + strtolower *
# ************************
# Time: 0.000551
# Memory: 1856
# ************************
# * strcasecmp *
# ************************
# Time: 0.000418
# Memory: 1856
#
#
# String length: 500
# ************************
# * strcomp + strtolower *
# ************************
# Time: 0.001677
# Memory: 2352
# ************************
# * strcasecmp *
# ************************
# Time: 0.001383
# Memory: 2352
#
# strcasecmp wins!
<?php
$n = isset($argv[1]) ? (int) $argv[1] : 200;
$before = memory_get_usage();
$start = microtime(true);
$a = '';
$b = '';
for ($i = 0; $i < $n; $i++) {
$a .= $i % 2 ? "A" : 'a';
$b .= 'a';
strcasecmp($a, $b);
}
$end = microtime(true);
$after = memory_get_usage();
printf("************************\n");
printf("* strcasecmp *\n");
printf("************************\n");
printf("Time: %f\n", $end - $start);
printf("Memory: %d\n", $after - $before);
<?php
$n = isset($argv[1]) ? (int) $argv[1] : 200;
$before = memory_get_usage();
$start = microtime(true);
$a = '';
$b = '';
for ($i = 0; $i < $n; $i++) {
$a .= $i % 2 ? "A" : 'a';
$b .= 'a';
strcmp(strtolower($a), strtolower($b));
}
$end = microtime(true);
$after = memory_get_usage();
printf("************************\n");
printf("* strcomp + strtolower *\n");
printf("************************\n");
printf("Time: %f\n", $end - $start);
printf("Memory: %d\n", $after - $before);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment