在哪里可以找到用于编写每个PHP“内置” PHP的算法?功能?

编程入门 行业动态 更新时间:2024-10-15 22:23:34
本文介绍了在哪里可以找到用于编写每个PHP“内置” PHP的算法?功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我最近构建了一个基于PHP的应用程序,通常需要几(> 10)秒才能解析目标字符串(> 10秒,因为对一个典型的100kB +字符串有成千上万的检查)。我正在寻找减少执行时间的方法。

I recently built a PHP-based application that typically requires several (>10) seconds to parse a target string (>10 seconds because there are many thousands of checks on a typically 100kB+ string). I am looking for ways to reduce the execution time.

我开始怀疑如何编写PHP的每个内置函数。例如,如果您转到手册(strpos()参考php rel = nofollow noreferrer>此链接),虽然有很多信息,但是没有算法。

I started to wonder how each of PHP's "built-in" functions are written. For example, if you go to the strpos() reference in the manual (this link), there is a lot of info but not the algorithm.

谁知道,也许我可以为我的特定应用程序编写一个比内置函数更快的函数?但是我没有办法知道例如strpos()。该算法是否使用这种方法:

Who knows, maybe I can write a function that is faster than the built-in function for my particular application? But I have no way of knowing the algorithm for e.g. strpos(). Does the algorithm use a method such as this one:

function strposHypothetical($haystack, $needle) { $haystackLength = strlen($haystack); $needleLength = strlen($needle);//for this question let's assume > 0 $pos = false; for($i = 0; $i < $haystackLength; $i++) { for($j = 0; $j < $needleLength; $j++) { $thisSum = $i + $j; if (($thisSum > $haystackLength) || ($needle[$j] !== $haystack[$thisSum])) break; } if ($j === $needleLength) { $pos = $i; break; } } return $pos; }

还是会使用慢得多的方法,比如说substr_count( ),如果出现的是针头,如果出现的次数> 0,则是for循环,还是其他方法?

or would it use a much slower method, with let's say combination of substr_count() for occurrences of the needle, and if occurrences > 0, then a for loop, or some other method?

我已经在应用程序中对函数和方法进行了概述,以这种方式取得重大进展。另外,请注意,此帖子并没有太大帮助。在哪里可以找到PHP中每个内置函数使用的算法,或者此信息是专有的?

I have profiled the functions and methods in my application and made significant progress in this way. Also, note that this post doesn't really help much. Where can I find out the algorithm used for each built-in function in PHP, or is this information proprietary?

推荐答案

内置-in PHP函数可在PHP源代码的 / ext / standard /中找到代码。

The built-in PHP functions can be found in /ext/standard/ in the PHP source code.

对于 strpos ,您可以在 / ext / standard / string.c 。实际上,此函数实际上使用 php_memnstr ,实际上是 zend_memnstr :

In the case of strpos, you can find the PHP implementation in /ext/standard/string.c. At its core, this function actually uses php_memnstr, which is actually an alias of zend_memnstr:

found = (char*)php_memnstr(ZSTR_VAL(haystack) + offset, Z_STRVAL_P(needle), Z_STRLEN_P(needle), ZSTR_VAL(haystack) + ZSTR_LEN(haystack));

如果我们阅读 zend_memnstr ,我们可以找到算法本身实施 strpos :

while (p <= end) { if ((p = (const char *)memchr(p, *needle, (end-p+1))) && ne == p[needle_len-1]) { if (!memcmp(needle, p, needle_len-1)) { return p; } } if (p == NULL) { return NULL; } p++; }

ne needle 和 p 的最后一个字符是一个指针,该指针递增以扫描 haystack 。

ne here represents the last character of needle, and p is a pointer which is incremented to scan through the haystack.

函数 memchr 是一个C函数,应该执行简单的线性运算搜索字节序列,以查找给定字节/字符在字节字符串中的首次出现。 memcmp 是一个C函数,它通过逐字节比较两个可以在字符串中的字节/字符范围来进行比较。

The function memchr is a C function which should do a simple linear search through a sequence of bytes to find the first occurrence of a given byte / character in a string of bytes. memcmp is a C function which compares two byte / character ranges which can be within strings by comparing them byte-by-byte.

此函数的伪代码版本如下:

A pseudo-code version of this function is as follows:

while (p <= end) { find the next occurrence of the first character of needle; if (occurrence is found) { set `p` to point to this new location in the string; if ((character at `p` + `length of needle`) == last character of needle) { if ((next `length of needle` characters after `p`) == needle) { return p; // Found position `p` of needle in haystack! } } } else { return NULL; // Needle does not exist in haystack. } p++; }

这是一种相当有效的算法,用于查找字符串中子字符串的索引。它与您的 strposHypothetical 几乎相同,并且在复杂度方面应同样有效,除非 memcpy 做到一旦看到字符串相差一个字符,就不会提早返回,并且当然,如果使用C语言实现,它将更精简,更快。

This is a fairly efficient algorithm for finding the index of a substring in a string. It is pretty much the same algorithm to your strposHypothetical, and should be just as efficient complexity-wise, unless memcpy doesn't return early as soon as it sees the strings differ by one character, and of course, being implemented in C, it will be leaner and faster.

更多推荐

在哪里可以找到用于编写每个PHP“内置” PHP的算法?功能?

本文发布于:2023-11-29 11:14:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646240.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可以找到   算法   功能   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!