Python与C++之间运行速度对比


很多人说python语言运行速度慢,那么我用一个遍历图片像素的例子做对比。

准备工作

  • 一张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]
    # 遍历所有像素并设置像素值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
# 只判断右下领域,值不同则保留当前值。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

总结

C++运行速度为0.25秒,而python需要足足23秒,有时更需要27秒,速度方面确实没有可比性,慢了两个数量级。为啥还那么多人用python呢?

  • 其实很多python库都是用C或C++实现的,而当我们只关注上层极少数逻辑代码时,那么它的运行速度并没有想象中的那么慢。
  • 从以上列子中很难看出python代码的简洁性,实际上python代码是非常简洁优雅的,花费少量时间写代码,运行时间可以通过并行GPU等手段优化,尤其在机器学习中,让我们有更多时间关注算法本身。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,718评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,683评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,207评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,755评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,862评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,050评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,136评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,882评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,330评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,651评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,789评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,477评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,135评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,864评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,099评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,598评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,697评论 2 351

推荐阅读更多精彩内容