Python與C++之間運(yùn)行速度對(duì)比


很多人說(shuō)python語(yǔ)言運(yùn)行速度慢,那么我用一個(gè)遍歷圖片像素的例子做對(duì)比。

準(zhǔn)備工作

  • 一張2048x1024大小的圖片


    image.png
  • opecv 2.0

  • xcode 8.3

  • python 2.7 PyCharm

C++代碼

//處理邊緣
Mat detectEdge(Mat &image)
{
    Mat resultImage = image.clone();
    int rows = resultImage.rows;
    int cols = resultImage.cols;
    for(int i = 0;i < rows;i++)
    {
        for(int j = 0;j < cols;j++)
        {
            //判斷下邊
            if(i!=rows-1&&resultImage.at<Vec4b>(i,j)!=resultImage.at<Vec4b>(i+1,j))
            {
                // road + sidewalk
                if (judgeHorizonPixels(resultImage, i, j, road, sidewalk)) {
                    resultImage.at<Vec4b>(i,j)=my_road_sidewalk;
                }
                // road + car
                else if (judgeHorizonPixels(resultImage, i, j, road, car)) {
                    resultImage.at<Vec4b>(i,j)=my_road_car;
                }
                // road + pole
                else if (judgeHorizonPixels(resultImage, i, j, road, pole)) {
                    resultImage.at<Vec4b>(i,j)=my_road_pole;
                }
                // road
                else if(image.at<Vec4b>(i,j)==road||image.at<Vec4b>(i+1,j)==road){
                    resultImage.at<Vec4b>(i,j)=my_road;
                }
                
                // building + pole
                else if (judgeHorizonPixels(resultImage, i, j, building, pole)) {
                    resultImage.at<Vec4b>(i,j)=my_building_pole;
                }
                // building + sidewalk
                else if (judgeHorizonPixels(resultImage, i, j, building, sidewalk)) {
                    resultImage.at<Vec4b>(i,j)=my_building_sidewalk;
                }
                // building + traffic sign
                else if (judgeHorizonPixels(resultImage, i, j, building, traffic_sign)) {
                    resultImage.at<Vec4b>(i,j)=my_building_traffic_sign;
                }
                // building + car
                else if (judgeHorizonPixels(resultImage, i, j, building, car)) {
                    resultImage.at<Vec4b>(i,j)=my_building_car;
                }
                // building + vegetation
                else if (judgeHorizonPixels(resultImage, i, j, building, vegetation)) {
                    resultImage.at<Vec4b>(i,j)=my_building_vegetation;
                }
                // building + sky
                else if (judgeHorizonPixels(resultImage, i, j, building, sky)) {
                    resultImage.at<Vec4b>(i,j)=my_building_sky;
                }
                // building + person
                else if (judgeHorizonPixels(resultImage, i, j, building, person)) {
                    resultImage.at<Vec4b>(i,j)=my_building_person;
                }
                // building
                else if(image.at<Vec4b>(i,j)==building||image.at<Vec4b>(i+1,j)==building){
                    resultImage.at<Vec4b>(i,j)=my_building;
                }
                
                // pole + car
                else if (judgeHorizonPixels(resultImage, i, j, pole, car)) {
                    resultImage.at<Vec4b>(i,j)=my_pole_car;
                }
                // pole + vegetation
                else if (judgeHorizonPixels(resultImage, i, j, pole, vegetation)) {
                    resultImage.at<Vec4b>(i,j)=my_pole_vegetation;
                }
                else{
                    resultImage.at<Vec4b>(i,j)=(Vec4b){0,0,0,255};
                }
            }
            //判斷右邊
            else if(j!=cols-1&&resultImage.at<Vec4b>(i,j)!=resultImage.at<Vec4b>(i,j+1))
            {
                // road + sidewalk
                if (judgeVerticalPixels(resultImage, i, j, road, sidewalk)) {
                    resultImage.at<Vec4b>(i,j)=my_road_sidewalk;
                }
                // road + car
                else if (judgeVerticalPixels(resultImage, i, j, road, car)) {
                    resultImage.at<Vec4b>(i,j)=my_road_car;
                }
                // road + pole
                else if (judgeVerticalPixels(resultImage, i, j, road, pole)) {
                    resultImage.at<Vec4b>(i,j)=my_road_pole;
                }
                // road
                else if(image.at<Vec4b>(i,j)==road||image.at<Vec4b>(i,j+1)==road){
                    resultImage.at<Vec4b>(i,j)=my_road;
                }
                
                // building + pole
                else if (judgeVerticalPixels(resultImage, i, j, building, pole)) {
                    resultImage.at<Vec4b>(i,j)=my_building_pole;
                }
                // building + sidewalk
                else if (judgeVerticalPixels(resultImage, i, j, building, sidewalk)) {
                    resultImage.at<Vec4b>(i,j)=my_building_sidewalk;
                }
                // building + traffic sign
                else if (judgeVerticalPixels(resultImage, i, j, building, traffic_sign)) {
                    resultImage.at<Vec4b>(i,j)=my_building_traffic_sign;
                }
                // building + car
                else if (judgeVerticalPixels(resultImage, i, j, building, car)) {
                    resultImage.at<Vec4b>(i,j)=my_building_car;
                }
                // building + vegetation
                else if (judgeVerticalPixels(resultImage, i, j, building, vegetation)) {
                    resultImage.at<Vec4b>(i,j)=my_building_vegetation;
                }
                // building + sky
                else if (judgeVerticalPixels(resultImage, i, j, building, sky)) {
                    resultImage.at<Vec4b>(i,j)=my_building_sky;
                }
                // building + person
                else if (judgeVerticalPixels(resultImage, i, j, building, person)) {
                    resultImage.at<Vec4b>(i,j)=my_building_person;
                }
                // building
                else if(image.at<Vec4b>(i,j)==building||image.at<Vec4b>(i,j+1)==building){
                    resultImage.at<Vec4b>(i,j)=my_building;
                }
                
                // pole + car
                else if (judgeVerticalPixels(resultImage, i, j, pole, car)) {
                    resultImage.at<Vec4b>(i,j)=my_pole_car;
                }
                // pole + vegetation
                else if (judgeVerticalPixels(resultImage, i, j, pole, vegetation)) {
                    resultImage.at<Vec4b>(i,j)=my_pole_vegetation;
                }
                else{
                    resultImage.at<Vec4b>(i,j)=(Vec4b){0,0,0,255};
                }
            }else
            {
                resultImage.at<Vec4b>(i,j)=(Vec4b){0,0,0,255};
            }
        }
    }
    return resultImage;
}
int main(int argc, const char * argv[]) {
    
    Mat image=imread("/Users/gcf/Desktop/test.png",CV_LOAD_IMAGE_UNCHANGED);
    clock_t startTime,endTime;
    startTime = clock();
    Mat resultImage=detectEdge(image);
    endTime = clock();
    cout << "Totle Time : " <<(double)(endTime - startTime) / CLOCKS_PER_SEC << "s" << endl;
    imwrite("/Users/gcf/Desktop/22.png", resultImage);
    return 0;
}
image.png

Python代碼

# 處理邊緣
def edge_classifier(img):
    image2 = copy.deepcopy(img)
    rows = image2.shape[0]
    cols = image2.shape[1]
    # 遍歷所有像素并設(shè)置像素值BGRA
    for i in xrange(rows):
        for j in xrange(cols):
            # 判斷下邊
            if i != rows - 1 and (image2[i, j] == image2[i + 1, j]).all() == False:
                # road + sidewalk
                if judge_horizon_pixels(image2, i, j, road, sidewalk):
                    image2[i, j] = my_road_sidewalk
                # road + car
                elif judge_horizon_pixels(image2, i, j, road, car):
                    image2[i, j] = my_road_car
                # road + pole
                elif judge_horizon_pixels(image2, i, j, road, pole):
                    image2[i, j] = my_road_pole
                # road
                elif (image2[i, j] == road).all() or (image2[i + 1, j] == road).all():
                    image2[i, j] = my_road

                # building + pole
                elif judge_horizon_pixels(image2, i, j, building, pole):
                    image2[i, j] = my_building_pole
                # building + sidewalk
                elif judge_horizon_pixels(image2, i, j, building, sidewalk):
                    image2[i, j] = my_building_sidewalk
                # building + traffic sign
                elif judge_horizon_pixels(image2, i, j, building, traffic_sign):
                    image2[i, j] = my_building_traffic_sign
                # building + car
                elif judge_horizon_pixels(image2, i, j, building, car):
                    image2[i, j] = my_building_car
                # building + vegetation
                elif judge_horizon_pixels(image2, i, j, building, vegetation):
                    image2[i, j] = my_building_vegetation
                # building + sky
                elif judge_horizon_pixels(image2, i, j, building, sky):
                    image2[i, j] = my_building_sky
                # building + person
                elif judge_horizon_pixels(image2, i, j, building, person):
                    image2[i, j] = my_building_person
                # building
                elif (image2[i, j] == building).all() or (image2[i + 1, j] == building).all():
                    image2[i, j] = my_building

                # pole + car
                elif judge_horizon_pixels(image2, i, j, pole, car):
                    image2[i, j] = my_pole_car
                # pole + vegetation
                elif judge_horizon_pixels(image2, i, j, pole, vegetation):
                    image2[i, j] = my_pole_vegetation
                else:
                    image2[i, j] = [0, 0, 0, 255]
            # 判斷右邊
            elif j != cols - 1 and (image2[i, j] == image2[i, j + 1]).all() == False:
                # road + sidewalk
                if judge_vertical_pixels(image2, i, j, road, sidewalk):
                    image2[i, j] = my_road_sidewalk
                # road + car
                elif judge_vertical_pixels(image2, i, j, road, car):
                    image2[i, j] = my_road_car
                # road + pole
                elif judge_vertical_pixels(image2, i, j, road, pole):
                    image2[i, j] = my_road_pole
                # road
                elif (image2[i, j] == road).all() or (image2[i, j + 1] == road).all():
                    image2[i, j] = my_road

                # building + pole
                elif judge_vertical_pixels(image2, i, j, building, pole):
                    image2[i, j] = my_building_pole
                # building + sidewalk
                elif judge_vertical_pixels(image2, i, j, building, sidewalk):
                    image2[i, j] = my_building_sidewalk
                # building + traffic sign
                elif judge_vertical_pixels(image2, i, j, building, traffic_sign):
                    image2[i, j] = my_building_traffic_sign
                # building + car
                elif judge_vertical_pixels(image2, i, j, building, car):
                    image2[i, j] = my_building_car
                # building + vegetation
                elif judge_vertical_pixels(image2, i, j, building, vegetation):
                    image2[i, j] = my_building_vegetation
                # building + sky
                elif judge_vertical_pixels(image2, i, j, building, sky):
                    image2[i, j] = my_building_sky
                # building + person
                elif judge_vertical_pixels(image2, i, j, building, person):
                    image2[i, j] = my_building_person
                # building
                elif (image2[i, j] == building).all() or (image2[i, j + 1] == building).all():
                    image2[i, j] = my_building

                # pole + car
                elif judge_vertical_pixels(image2, i, j, pole, car):
                    image2[i, j] = my_pole_car
                # pole + vegetation
                elif judge_vertical_pixels(image2, i, j, pole, vegetation):
                    image2[i, j] = my_pole_vegetation
                else:
                    image2[i, j] = [0, 0, 0, 255]
            else:
                image2[i, j] = [0, 0, 0, 255]
    return image2
# 只判斷右下領(lǐng)域孽文,值不同則保留當(dāng)前值。RGBA
def detect_RGBA_edge():
    img = cv2.imread('//Users/gcf/Desktop/test.png',cv2.IMREAD_UNCHANGED)
    starttime = datetime.datetime.now()
    image2 = edge_classifier(img)
    endtime = datetime.datetime.now()
    print 'Totle Time : %ds'%(endtime-starttime).seconds
    cv2.imwrite('/Users/gcf/Desktop/11.png', image2)
image.png

image.png

總結(jié)

C++運(yùn)行速度為0.25秒剃盾,而python需要足足23秒蛆挫,有時(shí)更需要27秒,速度方面確實(shí)沒(méi)有可比性域帐,慢了兩個(gè)數(shù)量級(jí)赘被。為啥還那么多人用python呢?

  • 其實(shí)很多python庫(kù)都是用C或C++實(shí)現(xiàn)的肖揣,而當(dāng)我們只關(guān)注上層極少數(shù)邏輯代碼時(shí)民假,那么它的運(yùn)行速度并沒(méi)有想象中的那么慢。
  • 從以上列子中很難看出python代碼的簡(jiǎn)潔性龙优,實(shí)際上python代碼是非常簡(jiǎn)潔優(yōu)雅的羊异,花費(fèi)少量時(shí)間寫代碼,運(yùn)行時(shí)間可以通過(guò)并行GPU等手段優(yōu)化彤断,尤其在機(jī)器學(xué)習(xí)中野舶,讓我們有更多時(shí)間關(guān)注算法本身。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末宰衙,一起剝皮案震驚了整個(gè)濱河市平道,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌供炼,老刑警劉巖一屋,帶你破解...
    沈念sama閱讀 218,607評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異袋哼,居然都是意外死亡冀墨,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門先嬉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)轧苫,“玉大人,你說(shuō)我怎么就攤上這事疫蔓『茫” “怎么了?”我有些...
    開封第一講書人閱讀 164,960評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵岔乔,是天一觀的道長(zhǎng)滚躯。 經(jīng)常有香客問(wèn)我嘿歌,道長(zhǎng),這世上最難降的妖魔是什么茁影? 我笑而不...
    開封第一講書人閱讀 58,750評(píng)論 1 294
  • 正文 為了忘掉前任宙帝,我火速辦了婚禮募闲,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘浩螺。我一直安慰自己靴患,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,764評(píng)論 6 392
  • 文/花漫 我一把揭開白布要出。 她就那樣靜靜地躺著鸳君,像睡著了一般患蹂。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上饭宾,一...
    開封第一講書人閱讀 51,604評(píng)論 1 305
  • 那天格了,我揣著相機(jī)與錄音徽鼎,去河邊找鬼盛末。 笑死否淤,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的檐嚣。 我是一名探鬼主播啰扛,決...
    沈念sama閱讀 40,347評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼鞍帝!你這毒婦竟也來(lái)了煞茫?” 一聲冷哼從身側(cè)響起摄凡,我...
    開封第一講書人閱讀 39,253評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤蚓曼,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后纫版,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,702評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡会涎,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,893評(píng)論 3 336
  • 正文 我和宋清朗相戀三年瑞凑,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片练慕。...
    茶點(diǎn)故事閱讀 40,015評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡技掏,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出哑梳,到底是詐尸還是另有隱情,我是刑警寧澤悯仙,帶...
    沈念sama閱讀 35,734評(píng)論 5 346
  • 正文 年R本政府宣布吠卷,位于F島的核電站,受9級(jí)特大地震影響祭隔,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜千贯,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,352評(píng)論 3 330
  • 文/蒙蒙 一程奠、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧己沛,春花似錦慌核、人聲如沸申尼。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)霹粥。三九已至,卻和暖如春后控,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背捌朴。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工张抄, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人左驾。 一個(gè)月前我還...
    沈念sama閱讀 48,216評(píng)論 3 371
  • 正文 我出身青樓泽台,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親怀酷。 傳聞我的和親對(duì)象是個(gè)殘疾皇子嗜闻,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,969評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,152評(píng)論 25 707
  • 文/Bruce.Liu1 1.Python前世今生 1.1.Python歷史 Python的創(chuàng)始人: Guido ...
    BruceLiu1閱讀 11,812評(píng)論 3 120
  • 關(guān)于和時(shí)間建立關(guān)系檐束,我更喜歡適合自己的束倍,并且行之有效的方法盟戏。我嘗試與時(shí)間建立聯(lián)系的方法就是:記錄時(shí)間甥桂。就像記錄金錢...
    蘇格拉底閱讀 247評(píng)論 1 5
  • 今天接到了一個(gè)老朋友的電話民镜,與其說(shuō)是朋友蜂嗽,更不如貼切地說(shuō)是老同學(xué)殃恒,我與他已經(jīng)好久都不聯(lián)系,只是偶爾會(huì)在朋友圈里看見...
    狐心雪閱讀 161評(píng)論 2 1
  • 1. 在當(dāng)今社會(huì),我們總是會(huì)被工作所困嵌戈。加班,出差熟呛,這些都是時(shí)常有的事情。經(jīng)常連拿起手機(jī)問(wèn)好身邊人的機(jī)會(huì)都沒(méi)有吗冤,減...
    _劉建宏_閱讀 1,431評(píng)論 8 46