Jump to content

[SOLVED] Most efficient way to perform case-irrelevant comparison


jordanwb

Recommended Posts

In my User class' Register function it calls a function that checks if an already existing user has a field whose value is the same as the one that the new user is trying to use. An example is Email address. Now I want to perform a comparison where case does not matter. Now I know how I would do that, but that's not the question:

 

[1]: strtolower ("string") == strlower ("STRING")

[2]: preg_match ("#^[string]$#i", "STRING")

 

But what I'm asking is which would be more efficient? Is there perhaps another algorithm that would be better?

probably the fastest way of comparing two strings case insensitively is with strcasecmp(). Keep in mind that this function won't return what is immediately expected in that it returns 0 if the strings are equal. I'm only guessing that it's faster since it's one function call instead of two, and regex is inherently slow, so I may be mistaken in it being the fastest alternative :P. Benchmark if you really care about it being the perfect alternative.

 

<?php
$str1 = 'stuff';
$str2 = 'stUfF';

if(strcasecmp($str1, $str2) == 0)
{
   echo "$str1, $str2 ARE equal to eachother!";
}
?>

Benchmark if you really care about it being the perfect alternative.

 

That's not a bad idea:

 

<?php

$string_1 = "Jordan";
$string_2 = "jORDAN";

$tests = 100000;
$start = microtime ();

for ($i = 0; $i < $tests; $i++)
{
strcasecmp ($string_1, $string_2);
}

print "strcasecmp took: " . (microtime () - $start) . " microseconds.<br />";

$start = microtime ();

for ($i = 0; $i < $tests; $i++)
{
strtolower ($string_1) == strtolower ($string_2);
}

print "strtolower took: " . (microtime () - $start) . " microseconds.<br />";

$start = microtime ();

for ($i = 0; $i < $tests; $i++)
{
preg_match ("#^[".$string_1."]$#i", $string_2);
}

print "preg_match took: " . (microtime () - $start) . " microseconds.<br />";

?>

 

Output:

 

strcasecmp took: 0.165722 microseconds.

strtolower took: -0.668401 microseconds.

preg_match took: 0.361707 microseconds.

 

I'm not sure what to think regarding strtolower. I re-ran it a bunch of times and the output that was negative kept changing.

 

strcasecmp took: 0.166892 microseconds.

strtolower took: 0.334652 microseconds.

preg_match took: 0.374544 microseconds.

 

in these results strcasecmp seems the quickest.

<?php

$string_1 = "Jordan";
$string_2 = "jORDAN";

$tests = 100000;
$start = microtime (true);

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



strcasecmp ($string_1, $string_2);
}

print "strcasecmp took: " . (microtime (true) - $start) . " microseconds.<br />";

$start = microtime (true);

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



strtolower ($string_1) == strtolower ($string_2);
}

print "strtolower took: " . (microtime (true) - $start) . " microseconds.<br />";

$start = microtime (true);

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



preg_match ("#^[".$string_1."]$#i", $string_2);
}

print "preg_match took: " . (microtime (true) - $start) . " microseconds.<br />";

?>

 

Try specifying TRUE as the first argument to microtime.  I get the following from doing that:

strcasecmp took: 0.287498950958 microseconds.<br />
strtolower took: 0.728889942169 microseconds.<br />
preg_match took: 0.907059192657 microseconds.<br />

 

EDIT: The line breaks are there because I ran this in the shell.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.