EnexoOnoma
EnexoOnoma

Reputation: 8836

Which one of these two "find in array" solutions runs faster on a bigger scenario?

I would like to know, out of experience, which one of these two solutions are faster to execute when I have 100 keywords in the array?

Thank you

1st case

$string = 'My name is tom';
$array = array("name", "tom");
if(0 < count(array_intersect(explode(' ', strtolower($string)), $array))) {
  echo 'found';
}

2nd case

function in_string ($words, $string)
{

    $isFound = false;
    foreach ($words as $value) {
        $isFound = $isFound || (stripos($string, $value) !== false);
        if ($isFound) break; 
    }
return $isFound;
}

$string = 'My name is tom';
$array = array("name", "tom");
echo in_string($array,$string);

Upvotes: 1

Views: 57

Answers (3)

pleinx
pleinx

Reputation: 626

Check it by yourself, try this code and add your versions:

<?php 

function testMe (callable $func, $testName) {

    $start = microtime(true);
    echo '### START TEST <strong>['.$testName.']</strong> ###<br>';

    $i = 0;
    while($i < 100000) {
        $func();
        $i++;
    }   

    echo '### FINSIHED TEST <strong>['.$testName.']</strong> in ['.number_format(( microtime(true) - $start), 4).'sec] ###<br><br>';
}

// my own suggestation
testMe(function() {
   $search = ['name', 'tom'];
   $string = 'My name is tom';
   $content = explode(' ', $string);
   $found = array_map(function ($value) use ($string, $content) {
        return in_array($value, $content);
   }, $search);
}, 'in_array / array_map');


function in_string ($words, $string) {
    $isFound = false;
    foreach ($words as $value) {
        $isFound = $isFound || (stripos($string, $value) !== false);
        if ($isFound) break; 
    }
    return $isFound;
}

testMe(function() {
    $string = 'My name is tom';
    $array = array("name", "tom");
    in_string($array,$string);
}, 'in_string');


testMe(function() {
    $string      = 'My name is tom';
    $arr         = explode(' ', $string);
    $arr         = array_flip($arr);
    $searchwords = array("name", "tom");

    foreach ($searchwords as $key => $word ) {
        if( isset($arr[ $word ]) ) {
            //echo 'found';
        }
    }
}, 'foreach/isset');


testMe(function() {
    $string = 'My name is tom';
    $array = array("name", "tom");
    if(0 < count(array_intersect(explode(' ', strtolower($string)), $array))) {
        //echo 'found';
    }
}, 'array_intersect/explode');

Results:

PHP 5.6

### START TEST [in_array / array_map] ###
### FINSIHED TEST [in_array / array_map] in [0.5283sec] ###

### START TEST [in_string] ###
### FINSIHED TEST [in_string] in [0.1444sec] ###

### START TEST [foreach/isset] ###
### FINSIHED TEST [foreach/isset] in [0.2247sec] ###

### START TEST [array_intersect/explode] ###
### FINSIHED TEST [array_intersect/explode] in [0.2923sec] ###

PHP7.0

### START TEST [in_array / array_map] ###
### FINSIHED TEST [in_array / array_map] in [0.1875sec] ###

### START TEST [in_string] ###
### FINSIHED TEST [in_string] in [0.0503sec] ###

### START TEST [foreach/isset] ###
### FINSIHED TEST [foreach/isset] in [0.0940sec] ###

### START TEST [array_intersect/explode] ###
### FINSIHED TEST [array_intersect/explode] in [0.1423sec] ###

Simple testing maybe here http://phptester.net/

Upvotes: 1

RST
RST

Reputation: 3925

$string      = 'My name is tom';
$arr         = explode(' ', $string);
$arr         = array_flip($arr);
$searchwords = array("name", "tom");

foreach ($searchwords as $key => $word ) {
    if( isset($arr[ $word ]) ) {
      echo 'found';
}

This may need some finetuning to match the kind of strings you are using

Upvotes: 0

inubs
inubs

Reputation: 519

Why not to just write a simple 5min code, theres no big difference when having around 100 lines...

<?php
    function in_string ($words, $string)
    {
        $isFound = false;
        foreach ($words as $value) {
            $isFound = $isFound || (stripos($string, $value) !== false);
            if ($isFound) break; 
        }
        return "#";
    }
    $start = time();
    print("start: " . $start . "<br>");
    $c = 0;
    while ($c < 10000000) {
        $string = 'My name is tom';
        $array = array("name", "tom");
        if(0 < count(array_intersect(explode(' ', strtolower($string)), $array))) {
          echo '#';
        }
        $c++;
    }
    $end = time();
    print("<br>end: " . $end);

    $start = time();
    print("<br>start: " . $start . "<br>");
    $c = 0;
    while ($c < 10000000) {
        $string = 'My name is tom';
        $array = array("name", "tom");
        echo in_string($array,$string);
        $c++;
    }
    $end = time();
    print("<br>end: " . $end);
?>

output:

start: 1476878257
##...
end: 1476878273
start: 1476878273
##...
end: 1476878281

So make your own conclusions

Upvotes: 1

Related Questions