Python与C++之间运行速度对比
2018-02-27 本文已影响72人
逆风g
很多人说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等手段优化,尤其在机器学习中,让我们有更多时间关注算法本身。