云计算 - PHP 和 PHP MySQL

发布于 2024-10-17 14:31:45 字数 494 浏览 4 评论 0原文

我有一个用 PHP 编写的与 MySQL 通信的算法。在标准共享托管服务器上,计算大约需要 5 秒。我的问题是 - 我怎样才能加速它(当然不改变算法本身),使用专用/VPS,也许一些云计算或者......?

您有什么建议?

编辑:这是公共交通的Dijkstra算法。我(仅)有 3500 个独特的停靠点。

简而言之 - 我有多个数组用于记录活动停止、先前停止、lineToStop、totalTime、travelingTime 等。我循环获取停靠点的数量,获取停靠点的 ID,建立与其他停靠点的连接(基于该活动停靠点的时间),再次循环获取停靠点的大小()以找到具有最低值的停靠点,将其标记为激活并再次继续。

我意识到这是优化问题,所以 - > Dijkstra 算法优化/缓存

I have an algorithm written in PHP which communicates with MySQL. On standard shared hosting server it takes about ~5 seconds to calculate. My question is - how can I speed it up (of course without changing the algorithm itself), use dedicated/VPS, maybe some cloud computing or...?

What are your suggestions?

EDIT: It's Dijkstra algorithm for public transport. I have (only) 3500 unique stops.

In short - I have multiple arrays for recording active stops, previous stops, lineToStop, totalTime, travelingTime and so on. I loop for number of stops, get an id of a stop, build connections to other stops (based on the time on that active stop), loop again for size of stops() to find the one with the lowest value, mark it as active and continue again.

I realized it's optimizatio problem so - > Dijkstra algorithm optimization/caching

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

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

发布评论

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

评论(5

萌梦深 2024-10-24 14:31:45

当你购买机票时,第一个问题不是“你想去哪里?” - 是“你要哪里去?”换句话说,在没有太多太多细节的情况下,我们只是猜测。

“我怎样才能加快速度?”的最可靠答案无论如何,都是这样的:

测量它,找到实际瓶颈,消除瓶颈,重复直到它运行得足够好。

如果没有分析,您可能会浪费无尽的时间来优化代码的某些部分,而这些部分不会对性能产生任何重大影响(请注意对“实际瓶颈”的强调 - 许多程序员的世纪都花在优化某人想象 可能是瓶颈)。

示例:如果方法在程序启动时被调用一次,并且程序花费 90% 的时间等待磁盘 I/O,那么将方法加速 1000% 是没有意义的。另一个例子:当程序花费 90% 的时间等待 SQL 服务器运行复杂的、无索引的查询时,制作一个磁盘阵列来帮助解决 I/O 瓶颈。这些并不是您可能遇到的唯一问题,而且它们甚至不是相互排斥的 - 但在开始解决问题之前您需要知道遇到哪些问题。 “很慢”并不是对问题的描述,它只是一种症状(就像“头痛”可以是 9000 种不同医疗状况的症状一样)。


TL;DR:没有灵丹妙药

When you buy an airline ticket, the first question is not "where do you want to go?" - it is "where are you going from?" In other words, without much, much, MUCH more detail, we're just guessing.

The surest answer for "How can I speed it up?" in any case is this:

Measure it, find actual bottlenecks, remove bottlenecks, repeat until it runs well enough.

Without profiling, you could waste endless hours on optimizing some part of the code that doesn't have any significant performance impact (Note the emphasis on "actual bottlenecks" - many programmer-centuries were spent optimizing what someone imagined might be the bottleneck).

Example: speeding up a method by 1000% is pointless, if it's a method that gets called once at the start of your program, and the program spends 90% of its time waiting for disk I/O. Another example: making a disk array to help with I/O bottlenecks, when the program spends 90% of the time waiting for the SQL server which runs a complex, unindexed query. Those are not the only problems you could encounter, and they aren't even mutually exclusive - but you need to know what problems you have before you start solving one. "It is slow" is not a description of a problem, it is just a symptom (just like "a headache" can be a symptom of 9000 different medical conditions).


TL;DR: There is no silver bullet.

掌心的温暖 2024-10-24 14:31:45

您首先需要找到瓶颈在哪里。使用探查器 (xdebug) 并查看 PHP 中的哪些地方速度缓慢。可能大部分时间都在等待与 MySQL 的连接,或者 MySQL 进行处理,或者其他事情。如果缓慢实际上是在您的算法代码中,那么选项就是更改您的算法,这是您不希望的。或者投入更多的处理能力。但在考虑对其进行更多处理之前,我肯定会首先找出缓慢的具体位置。

You'll first need to find where the bottleneck is. Use a profiler (xdebug) and see where in your PHP the slowness is. It may be that the majority of the time is waiting for connectivity to MySQL, or MySQL doing processing, or something else. If the slowness is actually in your algorithm code, then options are change your algorithm, which you don't want. Or throw more processing power at it. But I'd definitely find out where exactly is the slowness first before looking at throwing more processing at it.

○愚か者の日 2024-10-24 14:31:45

“云计算”并不是什么可以提高你的计算能力的魔法。这是一项技术,您需要大幅重写算法才能使用它,而且很可能您根本不需要使用 SQL 数据库。

我认为您采用了更简单的方法:通过优化算法并调整数据库服务器。我确信,通过使用传统的优化方法,这样的时间可以减少 10 倍甚至更多。顺便说一句,你已经用过了吗?

"cloud computing" is not some magic that can boost your calculations. It's a technology and yo will need dramatically rewrite your algorithm to use it, and most probably you'll have to use no SQL database at all.

I'm think you have go much simpler way: by optimizing the algorithm and tuning your db server. I am sure that such time can be reduced by factor of 10 and even more than that, by using traditional optimization methods. Did you use any already, BTW?

貪欢 2024-10-24 14:31:45

您在循环中有一个数据库查询。您是否尝试过一次获取所有行,然后在内存中循环结果?如果您在 1 个页面加载中进行 3500 个查询,则速度可能会更快。

You have a database query inside a loop. Have you tried getting ALL the rows once, and looping through the results in memory? If you're making 3500 queries on 1 page load, it might be faster.

红颜悴 2024-10-24 14:31:45

根据算法,以及瓶颈是否在您的 php 代码中还是在数据库中,您可以尝试使用 php 编译器,例如 facebooks hiphop: http://developers.facebook.com/blog/post/358/

因为你没有很多流量,而是一种运行一次的算法,而且必须更快,您可能需要 php 编译器(如我链接的编译器),而不是操作码缓存器(如 bcompiler 或 similair),因为这仅在运行大量脚本时有帮助。

缓存的操作码所做的只是加快脚本的解析速度,但在您的情况下,脚本每 5 秒运行仅发生一次,并且可能不是瓶颈。然而,编译器将您的 php 代码转换为本机程序集,即使没有变得像交流一样快,但仍然比在 PHP 中运行平均快 2 倍。

同样,这取决于您的设计。如果脚本在这 5 秒的大部分时间里只是等待数据库,那么编译对您没有多大帮助。

我们在这里谈论什么样的算法?

Depending on the algorithm, and whether the bottleneck is in your php code or in the database, you could try using a php compiler, like for example facebooks hiphop: http://developers.facebook.com/blog/post/358/

Since you don't have a lot of traffic, but instead one algorithm that is run once, and that has to be faster, you probably need the php compiler (like the one I've linked), NOT an opcode cacher (like bcompiler or similair), since that only helps when running a lot of scripts.

All an opcode cached does is speed up the parsing of scripts, but in your case that happens only once per 5-second run of the script and is probably no the bottleneck. A compiler however, translates your php code to native assembly, which, even if not becoming as fast a c equivalent, is still averagely 2 times faster than runnning in PHP.

Again, this depends on your design. If the script just waits for the database most of the time of those 5 seconds, compiling won't help you much.

What kind of algorithm are we talking about here?

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