直觉告诉我,
fwrite在速度上应该会更快一些。毕竟
ofstream这样的c++输出流,通过引入了虚函数,简化了程
序员的编程逻辑,提高了安全性,但是也由于引入过多抽象层带来了相应的性能开销。
为了佐证我的想法,特地写了一个小程序来测试了一下,最终验证了我的揣测。
C程序代码如下:
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
double calcPeriod( const struct timeval start, const struct timeval end )
{
double dRate = 1e-6;
double dStart = start.tv_sec + start.tv_usec * dRate;
double dEnd = end.tv_sec + end.tv_usec * dRate;
return dEnd - dStart;
}
int main()
{
FILE * fp = fopen( "c.out", "w" );
int i = 0;
const char data = '1';
const int LIMIT = 1 << 24;
struct timeval tvStart;
struct timeval tvEnd;
gettimeofday( &tvStart, 0 );
while (i < LIMIT ) {
fwrite( &data, sizeof( data ), 1, fp );
++i;
}
fclose( fp );
gettimeofday( &tvEnd, 0 );
printf( "start: %d s:%d us/n", tvStart.tv_sec, tvStart.tv_usec );
printf( "end: %d s:%d us/n", tvEnd.tv_sec, tvEnd.tv_usec );
printf( "%lf second passed/n", calcPeriod( tvStart, tvEnd ) );
return 0;
}
实现相同功能的C++程序 :
#include <fstream>
#include <iostream>
#include <sys/time.h>
#include <time.h>
using namespace std;
double calcPeriod( const struct timeval& start, const struct timeval& end )
{
double dRate = 1e-6;
double dStart = start.tv_sec + start.tv_usec * dRate;
double dEnd = end.tv_sec + end.tv_usec * dRate;
return dEnd - dStart;
}
int main()
{
ofstream ofs( "cxx.out" );
int i = 0;
const int LIMIT = 1 << 24;
const char data = '1';
struct timeval tvStart;
struct timeval tvEnd;
gettimeofday( &tvStart, 0 );
while ( i < LIMIT) {
ofs << data;
++i;
}
ofs.close();
gettimeofday( &tvEnd, 0 );
cout << "start: " << tvStart.tv_sec << " second," << tvStart.tv_usec << " usecond" << endl;
cout << "end: " << tvEnd.tv_sec << " second," << tvEnd.tv_usec << " usecond" <<endl;
cout << calcPeriod( tvStart, tvEnd) << " second passed" << endl;
return 0;
}
使用g++ -O3命令行选项编译这两个版本的程序,然后分别运行7次。
发现使用fwrite方式写入16M字节文件平均需要0.754秒的时间,使用ofstream则平均耗时1.38秒。
序员的编程逻辑,提高了安全性,但是也由于引入过多抽象层带来了相应的性能开销。
为了佐证我的想法,特地写了一个小程序来测试了一下,最终验证了我的揣测。
C程序代码如下:
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
double calcPeriod( const struct timeval start, const struct timeval end )
{
double dRate = 1e-6;
double dStart = start.tv_sec + start.tv_usec * dRate;
double dEnd = end.tv_sec + end.tv_usec * dRate;
return dEnd - dStart;
}
int main()
{
FILE * fp = fopen( "c.out", "w" );
int i = 0;
const char data = '1';
const int LIMIT = 1 << 24;
struct timeval tvStart;
struct timeval tvEnd;
gettimeofday( &tvStart, 0 );
while (i < LIMIT ) {
fwrite( &data, sizeof( data ), 1, fp );
++i;
}
fclose( fp );
gettimeofday( &tvEnd, 0 );
printf( "start: %d s:%d us/n", tvStart.tv_sec, tvStart.tv_usec );
printf( "end: %d s:%d us/n", tvEnd.tv_sec, tvEnd.tv_usec );
printf( "%lf second passed/n", calcPeriod( tvStart, tvEnd ) );
return 0;
}
实现相同功能的C++程序 :
#include <fstream>
#include <iostream>
#include <sys/time.h>
#include <time.h>
using namespace std;
double calcPeriod( const struct timeval& start, const struct timeval& end )
{
double dRate = 1e-6;
double dStart = start.tv_sec + start.tv_usec * dRate;
double dEnd = end.tv_sec + end.tv_usec * dRate;
return dEnd - dStart;
}
int main()
{
ofstream ofs( "cxx.out" );
int i = 0;
const int LIMIT = 1 << 24;
const char data = '1';
struct timeval tvStart;
struct timeval tvEnd;
gettimeofday( &tvStart, 0 );
while ( i < LIMIT) {
ofs << data;
++i;
}
ofs.close();
gettimeofday( &tvEnd, 0 );
cout << "start: " << tvStart.tv_sec << " second," << tvStart.tv_usec << " usecond" << endl;
cout << "end: " << tvEnd.tv_sec << " second," << tvEnd.tv_usec << " usecond" <<endl;
cout << calcPeriod( tvStart, tvEnd) << " second passed" << endl;
return 0;
}
使用g++ -O3命令行选项编译这两个版本的程序,然后分别运行7次。
发现使用fwrite方式写入16M字节文件平均需要0.754秒的时间,使用ofstream则平均耗时1.38秒。