jordanwb Posted August 31, 2008 Share Posted August 31, 2008 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? Link to comment https://forums.phpfreaks.com/topic/122058-solved-most-efficient-way-to-perform-case-irrelevant-comparison/ Share on other sites More sharing options...
genericnumber1 Posted August 31, 2008 Share Posted August 31, 2008 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 . 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!"; } ?> Link to comment https://forums.phpfreaks.com/topic/122058-solved-most-efficient-way-to-perform-case-irrelevant-comparison/#findComment-630095 Share on other sites More sharing options...
JasonLewis Posted August 31, 2008 Share Posted August 31, 2008 I don't think there would be to much difference. I think I convert both to lowercase before comparison. I think. Link to comment https://forums.phpfreaks.com/topic/122058-solved-most-efficient-way-to-perform-case-irrelevant-comparison/#findComment-630098 Share on other sites More sharing options...
jordanwb Posted August 31, 2008 Author Share Posted August 31, 2008 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. Link to comment https://forums.phpfreaks.com/topic/122058-solved-most-efficient-way-to-perform-case-irrelevant-comparison/#findComment-630108 Share on other sites More sharing options...
DarkWater Posted August 31, 2008 Share Posted August 31, 2008 <?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. Link to comment https://forums.phpfreaks.com/topic/122058-solved-most-efficient-way-to-perform-case-irrelevant-comparison/#findComment-630111 Share on other sites More sharing options...
jordanwb Posted August 31, 2008 Author Share Posted August 31, 2008 Aww the process no longer goes back in time. :'( Link to comment https://forums.phpfreaks.com/topic/122058-solved-most-efficient-way-to-perform-case-irrelevant-comparison/#findComment-630385 Share on other sites More sharing options...
DarkWater Posted August 31, 2008 Share Posted August 31, 2008 Aww the process no longer goes back in time. :'( Lol. >_< You get the proper results from the first parameter being true. =P Link to comment https://forums.phpfreaks.com/topic/122058-solved-most-efficient-way-to-perform-case-irrelevant-comparison/#findComment-630390 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.