您现在的位置是:主页 > news > 做二手车放在哪个网站好/新闻稿发布软文平台
做二手车放在哪个网站好/新闻稿发布软文平台
admin2025/5/6 2:58:23【news】
简介做二手车放在哪个网站好,新闻稿发布软文平台,东莞樟木头网站建设,织梦cms建站本文主要借助我在项目中编写的一个小软件,来对一窥C实现和Python实现在“编码效率”、“运行效率”、“内存占用”等方面的差异。当然,这个对比只是针对这类项目,不具普遍性。 一、问题 将两个几M大小的文本格式txt文件(一个存储的…
本文主要借助我在项目中编写的一个小软件,来对一窥C++实现和Python实现在“编码效率”、“运行效率”、“内存占用”等方面的差异。当然,这个对比只是针对这类项目,不具普遍性。
一、问题
将两个几M大小的文本格式txt文件(一个存储的是float型的I路数据,另一个存储的是float型的Q路数据)合成一个二进制格式的bin文件(波形文件)。以本次实验的“bt_wave_i.txt”和“bt_wave_q.txt”为例:
如上图所示,这是用notepad++打开的部分数据截图,灰色部分是行号。两个文件的数据行是相等的,大约有450,000行。
二、对比
1,性能对比
上图是使用Process Explorer捕获的C++程序和python程序运行高峰期的性能截图。左图是C++的,看的是Release版本,右图是python的。从两图对比来看,性能上差异不大。
2,效率对比
上图是通过在代码中计时,打印核心处理过程的时间开销。
左图是C++的,release版本,step1和step2分别是读两个文件的时间—— 4.8s多,总计用时“9.66553s”。
右图是python,它使用的是“Parallel Iteration”,一次对多个文件,总用时“4.221034s”。
很明显:python的“Parallel Iteration”方式读读个文件,效率更高。C++如果能采用类似这种“Parallel Iteration”的方式,应该可以减少一部分时间。
此外,观察这个C++程序中的读文件(文本格式)和写文件(二进制格式)的时间开销,很容易发现,写二进制格式文件时,时间开销相对文本格式小很多。9.66553 - 4.82304 - 4.83301 = 0.00948 s。事实上,将IQ数据转为二进制的波形文件,就是要提高后续程序对该文件的读取速度。
3,代码对比
C++代码共109行(计空行);Python代码25行(计空行)。
1)C++代码
// txt2binary.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"#include <iostream>
#include <fstream>
#include <stdint.h>
#include <vector>
#include<chrono>
using namespace std;
using namespace chrono;
#define FILE_NAME_LENGTH 64
#define HEAD_LENGTH 8int main()
{char arrIFile[FILE_NAME_LENGTH]{ 0 };char arrQFile[FILE_NAME_LENGTH]{ 0 };const char* sfFile = "wave.iqbin";const char* txtFile = "wave.txt";cout << "Please enter i file name:\n";cin >> arrIFile;cout << "Please enter q file name:\n";cin >> arrQFile;cout << "Processing...\n";auto start = system_clock::now();vector<float> fIVector{ };vector<float> fQVector{ };float fTemp = 0;// read i dataifstream inIFile(arrIFile, std::ios::in);if (inIFile.is_open()){while (!inIFile.eof()){inIFile >> fTemp;fIVector.push_back(fTemp);}inIFile.close();}auto step1 = system_clock::now();auto dur1 = duration_cast<microseconds>(step1 - start);cout << "step 1 consume "<< double(dur1.count()) * microseconds::period::num / microseconds::period::den<< " seconds" << endl;fTemp = 0;// read q dataifstream inQFile(arrQFile, std::ios::in);if (inQFile.is_open()){while (!inQFile.eof()){inQFile >> fTemp;fQVector.push_back(fTemp);}inQFile.close();}auto step2 = system_clock::now();auto dur2 = duration_cast<microseconds>(step2 - step1);cout << "step 2 consume "<< double(dur2.count()) * microseconds::period::num / microseconds::period::den<< " seconds" << endl;int count = 0;float * buffer = NULL;if (fIVector.size() == fQVector.size()){count = fIVector.size() * 2 + HEAD_LENGTH;buffer = new float[count]{ 0 };for (int i = HEAD_LENGTH, j = 0; j < fIVector.size(); ++j){buffer[i] = fIVector[j];buffer[i + 1] = fQVector[j];i += 2;}}else{cout << "error: the size of i and q is not equal\n";return 0;}// write to binary filestd::ofstream out(sfFile, std::ios::out | std::ios::binary | std::ios::trunc);out.write((char*)buffer, sizeof(float)*count);out.close();auto end = system_clock::now();auto duration = duration_cast<microseconds>(end - start);cout << "consume "<< double(duration.count()) * microseconds::period::num / microseconds::period::den<< " seconds in all" << endl;cout << "Done\n";system("pause");return 0;
}
注:
1)C++代码还可以再优化,比如将读文件抽象为一个函数等。
2)该代码使用了一些C++11的特性,比如:vector和chrono。其中,C++的时间库,可以参考以下两篇博文:
http://www.cnblogs.com/qicosmos/p/3642712.html
http://blog.csdn.net/luotuo44/article/details/46854229
2,python代码
#txt2binPy
import struct
import time,datetimefilename1 = input('Please enter i file name:\t') or 'bt_wave_i.txt'
filename2 = input('Please enter q file name:\t') or 'bt_wave_q.txt'
f1 = open(filename1,'rt')
f2 = open(filename2,'rt')
fOut = open('wave.iqbin','wb')start = datetime.datetime.now()for line1, line2 in zip(f1, f2):iValue, qValue = float(line1), float(line2)iBytes, qBytes = struct.pack('f',iValue), struct.pack('f',qValue)fOut.write(iBytes)fOut.write(qBytes)end = datetime.datetime.now()
print('Done.')
print('consume %s seconds' % (end - start))f1.close()
f2.close()
fOut.close()
上述python代码用到了如下几个技巧:
1)“Parallel Iteration”,参考《Beginning Python》第5章,它介绍了几种“Iteration Utilities”,包括:“Parallel Iteration”、“Numbered Iteration”、“Reversed and Sorted Iteration”等。
2)“File Iterators”,参考《Beginning Python》第11章,无论是文件流还是fileinput,在python中都可以自然按行迭代。
3)python中的二进制转换。使用“struct”模块。参考博文:http://blog.csdn.net/lesky/article/details/5727471
4)python中的时间库。参考博文:
http://funhacks.net/2016/03/19/python%20时间戳处理/
http://blog.csdn.net/xiaobing_blog/article/details/12591917
http://5iqiong.blog.51cto.com/2999926/1110951
总结:很明显,从开发效率上来说,python高于C++。果然是:人生苦短,我用Python!!!