PHP函数执行成本表

发布于 2024-07-27 17:53:41 字数 800 浏览 6 评论 0原文

有一个参考表显示每个 php 函数的执行成本吗?

我知道时间执行受到许多因素的限制,并且不可能确定唯一的值,但我正在寻找一个“意识形态”表。

例如,

is_dir() = cost 3
is_file() = cost 2

(仅举个例子;)

如果我没记错的话,对于C来说,有一个表,其中包含每个操作所需的CPU周期。

编辑:我读了你所有的评论,所以有任何桌子可以满足我的需要。

无论如何,我知道

is_dir('/');//is faster than
is_dir('/a/very/long/path/to/check/');

但是,我很难接受这种情况(如果我正确理解了你的话,这是可能的);

$a = '/';
$b = '/a/very/long/path/to/check/';
is_dir($a);  //time execution 0.003
is_file($a); //time execution 0.005
//so suppose is_dir is faster than is_file
//(still example, function names and number are random ;)
is_dir($b);  //time execution 0.013
is_file($b); //time execution 0.009
//wow, now is faster is_file()....?

There is a reference table that shows the execution cost of every php function?

I know that the time execution is boundet to many factors and is impossible to determinate an unique value, but im looking for a 'ideological' table.

For example,

is_dir() = cost 3
is_file() = cost 2

(take it JUST as an example ;)

If i dont remember bad, for C there is a table with cpu cycles needed to every operation..

EDIT: i read all you comment, and so there are any table for my needs.

Anyway, i know that

is_dir('/');//is faster than
is_dir('/a/very/long/path/to/check/');

But, i have some trouble to accept this situation (that, if i have understood right your words, is possible);

$a = '/';
$b = '/a/very/long/path/to/check/';
is_dir($a);  //time execution 0.003
is_file($a); //time execution 0.005
//so suppose is_dir is faster than is_file
//(still example, function names and number are random ;)
is_dir($b);  //time execution 0.013
is_file($b); //time execution 0.009
//wow, now is faster is_file()....?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

翻身的咸鱼 2024-08-03 17:53:41

这样的图表没有帮助,也不准确。 在您的示例中,该功能的时间将在很大程度上取决于本地文件系统的详细信息,并且很快就会变得毫无意义。

衡量性能的唯一真正方法是构建代码,然后对其进行计时。 for 比 while 更快吗? 也许在一种情况下一个会比另一个更好,而另一个在另一种情况下会更快,这取决于特定于其中代码的各种因素。

使用分析器对代码进行计时,并获取一些对于您的服务器环境中的项目来说准确的有意义的数据。

Such a chart would not be helpful or accurate. In your example, the timing of the function would depend very much on the details of the local filing system and would quickly become meaningless.

The only true way to measure performance is to build your code, then time it. Is for faster than while? Maybe one will be better than the other in one situation, while the other would be faster in another situation, depending on all kinds of factors that would be specific to the code within.

Time your code, using a profiler, and get some meaningful data that is accurate for your project on your server environment.

毅然前行 2024-08-03 17:53:41

我不知道有这样的桌子。

函数的成本会根据输入的不同而有很大差异。

由 array_unique() 处理的 1000 项数组将比处理 5 项输入数组的相同函数花费更长的时间。

如果您有兴趣调整应用程序,您可以使用 microtime 来计时每个函数调用的执行时间。

它看起来像这样

$starttime = microtime();
is_dir('/var/foo');
$stoptime = microtime();
echo "is_dir cost: ".($stoptime-$startime);

I'm not aware of such a table.

The cost of a function will vary a lot depending on the input.

A 1000 item array being processed by array_unique() will take longer than the same function with a 5 item input array.

If you're interested in tuning your application you could use microtime to time the execution of each function call.

It would look something like this

$starttime = microtime();
is_dir('/var/foo');
$stoptime = microtime();
echo "is_dir cost: ".($stoptime-$startime);
亽野灬性zι浪 2024-08-03 17:53:41

我认为这样的表无论如何都不可能存在。

首先,时间很大程度上取决于输入。 但对于多种功能来说,这不是一个问题。 我们可以指出算法复杂度:O(n^2)、O(n log(n)) 等等。 我们有很好的数学工具来处理这样的事情。

更重要的是环境。 你无法预测功能会工作多久,因为你不知道所有的环境。 NFS 卷上的 file_exists() 速度不会像本地 ext3 卷上那么快。 您无法预测请求将传输到 MySQL 服务器等多长时间。

所以你应该做的就是测量、测量、再测量。 仅取决于您自己的测量结果和环境。 这是唯一的办法。

I think it's not possible that such table would exist anyhow.

First of all, time greatly depends on input. But it's not a such problem for variety of functions. We could point algorithmic complexity: O(n^2), O(n log(n)) and so on. We have great mathematical apparatus for dealing with such thing.

More important thing is environment. You could not predict how long will be working function, because you do not know all the environment. file_exists() on NFS volume will not be as fast as on local ext3 volume. You could not predict how long request will transfered to MySQL server and so on.

So what you should do — is measure, measure and measure. Depend only on your own measurements and your environment. It's the only way.

A君 2024-08-03 17:53:41

你不能像 PHP 那样“指令计数”。 即使使用 C,它也取决于 libc 实现。

You can't "instruction count" like that for PHP. Even with C it would depend on the libc implementation.

青春如此纠结 2024-08-03 17:53:41

我不认为存在函数成本表之类的东西,因为根据上下文不同,相同的函数可能具有非常不同的成本。

例如(但仅以它为例!):

is_dir('/'); //will be very fast
is_dir('/symbolic_link/symbolic_link_on_a_remote_fs/'); //will be way slower

话虽这么说,评估函数调用成本可能是一种非常好的优化方法。

使用探查器可以帮助您意识到代码中的某些特定函数调用比您希望的要慢,并且重新思考/重写这些部分可以大大加快速度。

I don't think there is such thing as a function costs table, because the same function can have very different costs depending on the context.

For example (but just take it as an example !) :

is_dir('/'); //will be very fast
is_dir('/symbolic_link/symbolic_link_on_a_remote_fs/'); //will be way slower

That being said, evaluating function calls costs can be a very good approach to optimization.

Working with a profiler can help you realize that some specific function calls in your code are slower than you'd like them to be, and re-thinking/re-writing these parts could speed things up a lot.

素食主义者 2024-08-03 17:53:41

您将获得的最接近的结果可能是通过 Vulcan Logic Dumper(作者的网页/项目主页链接)进行操作码分析,以前称为Vulcan Logic Disassembler(PECL 链接)。 Sarah Golemon 在她的博客上提供了一些关于这方面的好信息。

The closest that you'll get is likely OpCode analysis via the Vulcan Logic Dumper (author's webpage/project homepage link), which used to be called the Vulcan Logic Disassembler (PECL link). Sarah Golemon has some nice info about this stuff on her blog.

孤独难免 2024-08-03 17:53:41

使用register_tick_function并声明(ticks=1)
它们自 PHP4 以来就已存在,并提供低级指令计数。 我想象这是通过计算 PHP 操作码解释和执行期间生成的指令来实现的。

Use register_tick_function and declare(ticks=1)
These have been around since PHP4 and provide low-level instruction counting. I'm going to imagine this is achieved by counting the instructions generated during interpretation and execution of PHP opcodes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文