浮点运算,C/C++该怎么走?
我正在创建一个 Web 应用程序,它执行一些非常繁重的浮点算术计算,而且数量很多!我读了很多书,并且读到你可以创建 C(和 C++)函数并从 PHP 中调用它们,我想知道这样做是否会注意到速度的提高?
我想这样做,即使只是一秒钟的差异,除非它实际上更慢。
I'm creating a web application that does some very heavy floating point arithmetic calculations, and lots of them! I've been reading a lot and have read you can make C(and C++) functions and call them from within PHP, I was wondering if I'd notice a speed increase by doing so?
I would like to do it this way even if it's only a second difference, unless it's actually slower.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这完全取决于您正在进行的实际计算数量。如果您有数千次计算要做,那么编写一个扩展来为您处理它肯定是值得的。特别是,如果您有大量数据,这就是 PHP 真正失败的地方:它的内存管理器无法处理大量对象或大型数组(基于处理此类数据的经验)。
如果算法不是太难,您可能希望首先用 PHP 编写它。这为您提供了良好的参考速度,但更重要的是,它将有助于准确定义您需要在模块中实现的 API。
更新为“使用 6 个数字进行 75-100 次计算”。
如果您在每个页面加载时只执行一次此操作,我怀疑它不会成为整个加载时间的重要部分(当然取决于您还做了什么)。如果你多次调用这个函数,那么是的,即使 75 个操作也可能很慢——但是,由于你只使用 6 个变量,也许它们的优化器会做得很好(而使用 100 个变量时,几乎可以保证不会这样做)。
It all depends on the actual number of calculations you are doing. If you have thousands of calculations to do then certainly it will be worthwhile to write an extension to handle it for you. In particular, if you have a lot of data this is where PHP really fails: it's memory manager can't handle a lot of objects, or large arrays (based on experience working with such data).
If the algorithm isn't too difficult you may wish to write it in PHP first anyway. This gives you a good reference speed but more importantly it'll help define exactly what API you need to implement in a module.
Update to "75-100 calculations with 6 numbers".
If you are doing this only once per page load I'd suspect it won't be a significant part of the overall load time (depends what else you do of course). If you are calling this function many times then yes, even 75 ops might be slow -- however since you use only 6 variables perhaps their optimizer will do a good job (whereas with 100 variables it's pretty much guaranteed not to).
检查 SWIG。
Swig 是一种从 C 源代码中轻松创建 php(和其他语言)模块的方法。
Check SWIG.
Swig is a way to make php (and other languages) modules from your C sources rather easily.