加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Python比PHP慢,以获得索引位置

发布时间:2021-08-28 03:48:15 所属栏目:大数据 来源: https://www.jb51.cc
导读:我很惊讶地发现Python比PHP更慢以获得索引位置.有没有办法提高Python的性能? 例如: 在PHP中它花了:18.169965982437 ?PHP$time_start = microtime(true);$needle = file_get_contents("needle.wav", false, null, 46);foreach(range(0, 10000) as $num) {

我很惊讶地发现Python比PHP更慢以获得索引位置.有没有办法提高Python的性能?

例如:

在PHP中它花了:18.169965982437

<?PHP
$time_start = microtime(true);
$needle = file_get_contents("needle.wav", false, null, 46);

foreach(range(0, 10000) as $num) {
   $haystack = file_get_contents("haystack.wav");
   $match = strpos($haystack,$needle);
}

$time_end = microtime(true);

$finishTime = $time_end - $time_start;
echo $finishTime;
?>

在Python中它花了:23.6319999695

import time
import math
def microtime(get_as_float = False) :
    if get_as_float:
        return time.time()
    else:
        return '%f %d' % math.modf(time.time())

time_start = microtime(True)
needle = open("needle.wav","rb").read()

for x in range(0, 10000):
    haystack = open("haystack.wav","rb").read()
    match = haystack.index(needle[46:])

time_end = microtime(True)
finishTime = time_end - time_start
print finishTime

解决方法:

等效的python代码是这样的:

needle = open("needle.wav", "rb").read()[46:]

for x in range(0, 10000):
    haystack = open("haystack.wav", "rb").read()
    match = haystack.index(needle)

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读