3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Performs a words comparision between $s1 and $s2. This function counts every word only once, so 'hello hello' * would be 100% similar to 'hello'. Both strings will get converted to lowercase, with its diacritical marks * (´, ¨, ^, `, ~, etc.) stripped out. This behaviour is very much like a 'keywords search'. * @param string $s1 First string to compare * @param string $s2 Second string to compare * @param null|string[] $skipWords Value-only array of words that should'nt be taken into account when comparing $s1 and $s2 * @param null|string[] $skipText Value-only array of text that should'nt be taken into account when comparing $s1 and $s2. * Please note that this text will be stripped from the end, start and middle parts of words. * @return float|int Percent of similarity between $s1 and $s2, where 1 represents 100% and 0 represents 0% */ function compareWords($s1, $s2, $skipWords = [ 'en', 'de', 'del', 'los', 'la', 'in', 'from', 'the' ], $skipText = [ '.', ',', ';', ':' ]) { if ($s1 === null || $s2 === null) return 0; if ($skipText !== null && count($skipText) > 0) { $s1 = str_replace($skipText, '', $s1); $s2 = str_replace($skipText, '', $s2); } if ($skipWords !== null && count($skipWords) > 0) { $skipWords = array_map(function ($item) { return preg_quote($item, '/'); }, $skipWords); $skipWordsRegex = '/(?:(?<=\s)|^)(?:' . implode('|', $skipWords) . ')(?:(?=\s)|$)/'; $s1 = preg_replace($skipWordsRegex, '', $s1); $s2 = preg_replace($skipWordsRegex, '', $s2); } $s1 = trim(UString::lowerCase(preg_replace('/\s+/', ' ', UString::removeDiacritics($s1)))); $s2 = trim(UString::lowerCase(preg_replace('/\s+/', ' ', UString::removeDiacritics($s2)))); if (strlen($s1) === 0 || strlen($s2) === 0) return 0; $s1Words = array_unique(explode(' ', $s1)); $s2Words = array_unique(explode(' ', $s2)); $s1WordsCount = count($s1Words); $s2WordsCount = count($s2Words); // make sure $s1Words is the smaller array, to have a smaller cycle if ($s1WordsCount > $s2WordsCount) { $temp = $s1Words; $s1Words = $s2Words; $s2Words = $temp; } $s2Words = array_flip($s2Words); $maxWords = max($s1WordsCount, $s2WordsCount); $matches = 0; foreach ($s1Words as $s1Word) { if (array_key_exists($s1Word, $s2Words)) $matches++; } return $matches / $maxWords; } var_dump(compareWords('The tomato sauce.', 'tomato sauce'));

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0090.00618.54
8.3.50.0120.00316.50
8.3.40.0060.00919.05
8.3.30.0110.01119.14
8.3.20.0040.00422.02
8.3.10.0000.00823.65
8.3.00.0060.00319.56
8.2.180.0060.01018.58
8.2.170.0070.00722.96
8.2.160.0030.01020.47
8.2.150.0060.00324.18
8.2.140.0080.00024.66
8.2.130.0000.00726.16
8.2.120.0040.00422.34
8.2.110.0080.00322.44
8.2.100.0060.00617.97
8.2.90.0000.00817.73
8.2.80.0060.00618.98
8.2.70.0000.00917.93
8.2.60.0060.00318.04
8.2.50.0050.00318.22
8.2.40.0030.00720.59
8.2.30.0080.00019.64
8.2.20.0000.00818.17
8.2.10.0000.00818.05
8.2.00.0080.00018.37
8.1.280.0090.00925.92
8.1.270.0040.00422.27
8.1.260.0030.00526.35
8.1.250.0060.00328.09
8.1.240.0030.00723.61
8.1.230.0040.00819.22
8.1.220.0000.00818.90
8.1.210.0050.00318.77
8.1.200.0060.00317.60
8.1.190.0030.00619.26
8.1.180.0060.00318.10
8.1.170.0000.00818.81
8.1.160.0050.00318.89
8.1.150.0050.00220.34
8.1.140.0000.00817.79
8.1.130.0000.00719.10
8.1.120.0050.00217.64
8.1.110.0050.00317.59
8.1.100.0000.00817.70
8.1.90.0070.00017.55
8.1.80.0060.00317.70
8.1.70.0050.00317.65
8.1.60.0000.00917.78
8.1.50.0030.00617.72
8.1.40.0040.00417.72
8.1.30.0030.00517.80
8.1.20.0030.00617.69
8.1.10.0040.00417.71
8.1.00.0090.00017.64
8.0.300.0030.00518.77
8.0.290.0080.00016.63
8.0.280.0030.00318.48
8.0.270.0050.00317.29
8.0.260.0000.00717.00
8.0.250.0040.00417.22
8.0.240.0030.00317.22
8.0.230.0070.00017.28
8.0.220.0000.00917.19
8.0.210.0000.00717.21
8.0.200.0030.00317.11
8.0.190.0000.00717.21
8.0.180.0030.00617.11
8.0.170.0030.00617.09
8.0.160.0000.00917.11
8.0.150.0000.00917.16
8.0.140.0070.00017.20
8.0.130.0030.00313.62
8.0.120.0030.00517.09
8.0.110.0050.00217.12
8.0.100.0050.00317.18
8.0.90.0040.00417.22
8.0.80.0070.00717.11
8.0.70.0050.00317.20
8.0.60.0020.00516.98
8.0.50.0040.00417.02
8.0.30.0150.00717.41
8.0.20.0130.00817.43
8.0.10.0040.00417.20
8.0.00.0120.00517.10
7.4.330.0000.00615.55
7.4.320.0030.00316.75
7.4.300.0060.00016.85
7.4.290.0000.00816.80
7.4.280.0080.00016.84
7.4.270.0000.00816.84
7.4.260.0040.00416.68
7.4.250.0000.00816.79
7.4.240.0040.00416.89
7.4.230.0050.00216.89
7.4.220.0000.00716.79
7.4.210.0090.00616.89
7.4.200.0000.00816.88
7.4.160.0110.00616.75
7.4.150.0050.01317.40
7.4.140.0120.00817.86
7.4.130.0110.00716.82
7.4.120.0110.00816.69
7.4.110.0070.01016.84
7.4.100.0170.00016.88
7.4.90.0060.01216.87
7.4.80.0160.00819.39
7.4.70.0090.01416.84
7.4.60.0080.01616.66
7.4.50.0130.00316.52
7.4.40.0130.00316.63
7.4.00.0080.00815.03
7.3.330.0000.00513.58
7.3.320.0030.00313.64
7.3.310.0050.00316.48
7.3.300.0030.00316.65
7.3.290.0090.00716.65
7.3.280.0120.00716.63
7.3.270.0140.00417.40
7.3.260.0110.00916.78
7.3.240.0110.00716.70
7.3.230.0060.00916.57
7.3.210.0170.00916.64
7.3.200.0070.01116.84
7.3.190.0030.01316.58
7.3.180.0120.01216.83
7.3.170.0040.01216.60
7.3.160.0100.00716.64
7.3.120.0090.00914.84
7.3.110.0070.01015.23
7.3.100.0100.00615.27
7.3.90.0030.01315.09
7.3.80.0000.01115.24
7.3.70.0000.01215.06
7.3.60.0080.00315.10
7.3.50.0090.00315.24
7.3.40.0000.01215.07
7.3.30.0060.00615.24
7.3.20.0040.00716.66
7.3.10.0040.01116.68
7.3.00.0050.00816.65
7.2.330.0100.00717.05
7.2.320.0110.01216.98
7.2.310.0130.01016.86
7.2.300.0100.00616.88
7.2.290.0040.01516.94
7.2.250.0060.01215.32
7.2.240.0070.01315.30
7.2.230.0090.00615.39
7.2.220.0000.00915.13
7.2.210.0100.00315.01
7.2.200.0060.00615.27
7.2.190.0100.00315.26
7.2.180.0000.01615.46
7.2.170.0040.01215.29
7.2.60.0110.00317.13
7.2.20.0070.01220.17
7.2.10.0110.01320.16
7.2.00.0070.01820.21
7.1.330.0030.01016.17
7.1.320.0000.01316.19
7.1.310.0030.01316.22
7.1.300.0030.00616.06
7.1.290.0110.00316.09
7.1.280.0070.00315.72
7.1.270.0060.00315.59
7.1.260.0110.00416.21
7.1.200.0060.00915.85
7.1.140.0130.01119.05
7.1.130.0080.01118.83
7.1.120.0080.01318.79
7.1.110.0060.01018.43
7.1.100.0100.00918.64
7.1.90.0100.00818.14
7.1.80.0070.01018.01
7.1.70.0090.01117.41
7.1.60.0260.01435.47
7.1.50.0310.01634.97
7.1.40.0270.01434.66
7.1.30.0200.01634.94
7.1.20.0280.00834.89
7.1.10.0070.01116.88
7.1.00.0080.00816.96

preferences:
51.09 ms | 400 KiB | 5 Q