drcigvoy48900 2011-11-09 11:18
浏览 38
已采纳

C ++映射查找性能与PHP数组查找性能

I can't understand the following and I'm hoping someone can shed some light on it for me:

In C++ if I create a vector of test data containing 2M different bits of text (testdata) then create a map using these strings as the index values, then look up all the values, like this:

 //Create test data
for(int f=0; f<loopvalue; f++)
{   
    stringstream convertToString;
    convertToString << f;
    string strf = convertToString.str();
    testdata[f] = "test" + strf;
}

    time_t startTimeSeconds = time(NULL);

   for(int f=0; f<2000000; f++) testmap[ testdata[f] ] = f; //Write to map
   for(int f=0; f<2000000; f++) result = testmap[ testdata[f] ]; //Lookup

   time_t endTimeSeconds = time(NULL);
   cout << "Time taken " << endTimeSeconds - startTimeSeconds << "seconds." << endl;

It takes 10 seconds.

If I do seemingly at least the same in PHP:

<?php
$starttime = time();
$loopvalue = 2000000;

//fill array
for($f=0; $f<$loopvalue; $f++)
{
    $filler = "test" . $f;
    $testarray[$filler] = $f;
}

//look up array
for($f=0; $f<$loopvalue; $f++)
{
    $filler = "test" . $f;
    $result = $testarray[$filler];
}

$endtime = time();
echo "Time taken ".($endtime-$starttime)." seconds.";
?>

...it takes only 3 seconds.

Given that PHP is written in C does anyone know how PHP achieves this much faster text index lookup?

Thanks C

  • 写回答

3条回答 默认 最新

  • douzhan5058 2011-11-09 12:15
    关注

    I suspect you benchmark the wrong things. Anyway, I used your code (had to make some assumptions on your data types) and here are the results from my machine:

    PHP: Time taken 2 seconds.

    C++ (using std::map): Time taken 3 seconds.

    C++ (using std::tr1::unordered_map): Time taken 1 seconds.

    C++ compiled with

    g++ -03
    

    Here is my test C++ code:

    #include <map>
    #include <sstream>
    #include <string>
    #include <vector>
    #include <iostream>
    #include <tr1/unordered_map>
    
    
    int main(){
        const int loopvalue=2000000;
        std::vector<std::string> testdata(loopvalue);
        std::tr1::unordered_map<std::string, int> testmap;
        std::string result;
        for(int f=0; f<loopvalue; f++)
        {   
            std::stringstream convertToString;
            convertToString << f;
            std::string strf = convertToString.str();
            testdata[f] = "test" + strf;
        }
    
        time_t startTimeSeconds = time(NULL);
    
        for(int f=0; f<loopvalue; f++) testmap[ testdata[f] ] = f; //Write to map
        for(int f=0; f<loopvalue; f++) result = testmap[ testdata[f] ]; //Lookup
    
        time_t endTimeSeconds = time(NULL);
        std::cout << "Time taken " << endTimeSeconds - startTimeSeconds << "seconds." << std::endl;
    }
    

    Conclusion:

    You tested unoptimized C++ code, probably even compiled with VC++, which by default has a bounds check in std::vector::operator[] when compiled in debug mode.

    There still is a difference of PHP to the optimised C++ code, when we use std::map, because of the difference in lookup complexity (see n0rd's answer), but C++ is faster when you use a Hashmap.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看