注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

gmd20的个人空间

// 编程和生活

 
 
 

日志

 
 

C++ 流操作这么慢???比直接内存范围慢1000倍啊?  

2012-07-30 00:09:00|  分类: 程序设计 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

怀疑程序的性能比较差是因为使用了c++的流操作引起的,一个解码和编码的程序大量使用流操作来访问内存,结果最后的网络通讯数据只有1点多M左右,应该是是这些解码编码的操作占去了cpu了?


流操作比普通的内存访问慢1000倍以上啊? 到底都在干了些什么去了?  测试代码和结果如下

C++ 流操作这么慢???比直接内存范围慢1000倍啊? - widebright - widebright的个人空间

// test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include<sstream>
#include<string>
#include <iostream>
#include <ostream>
#include <iomanip>

#include <windows.h>
using namespace std;

class A {
public:
A (stringbuf &s):buf(&s){
}

void ResetG(){
buf.seekg(0,std::ios::beg);
}

void ResetP(){
buf.seekp(0,std::ios::beg);
}


A& write_8(const char &i)
{
char x(i);
buf.write(reinterpret_cast<char*>(&x), sizeof(char));
/*if (buf.fail()) cout << "PDU failed to write uint8_t";*/
return *this;
}

A& write_16(const short &i)
{
short j = i ; //htons(i);
buf.write(reinterpret_cast<char*>(&j), sizeof(short));
//if (buf.fail()) cout <<"PDU failed to write uint16_t";
return *this;
}

A& write_32(const int &i)
{
int j = i; //htonl(i);
buf.write(reinterpret_cast<char*>(&j), sizeof(int));
/*if (buf.fail()) cout <<"PDU failed to write uint32_t";*/
return *this;
}

private:
std::iostream buf;
};

class B {
public:
B (char * b) {
buf = b;
}
void ResetG(){
curg = buf;
}

void ResetP(){
curp =buf;
}


void write_8(const char &i)
{
*curp = i;
curg ++;
}

void write_16(const short &i)
{
short * sh = reinterpret_cast<short*>(curp);
* sh = i;
curp +=2;
}

void write_32(const int &i)
{
int * sh = reinterpret_cast<int*>(curp);
*sh = i;
curp +=4;
}

char * curg, *curp;
char * buf;

};

inline unsigned __int64 GetCycleCount()
{
//__asm RDTSC
return GetTickCount();

}

int _tmain(int argc, _TCHAR* argv[])
{
static char s[1024];
stringbuf ss("ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd");


unsigned long t1; unsigned long t2;

char ch = 1;
short sh = 2;
int in = 3;
A a(ss);

t1 = (unsigned long)GetCycleCount();
for (int i =0; i< 10000000; i++ ) {
a.ResetP();
a.write_8(ch);
a.write_16(sh);
a.write_32(in);
a.write_8(ch);
a.write_16(sh);
a.write_32(in);
}
t2 = (unsigned long)GetCycleCount();
cout << "用时: " << t2 - t1;
cout <<" " << ss.str();
cout <<" " << s[0] << s[1] << s[2] << endl;

B b(s);

t1 = (unsigned long)GetCycleCount();
for (int i =0; i< 10000000; i++ ) {
b.ResetP();
b.write_8(ch);
b.write_16(sh);
b.write_32(in);
b.write_8(ch);
b.write_16(sh);
b.write_32(in);
}
t2 = (unsigned long)GetCycleCount();
cout << "用时: " << t2 - t1;
cout <<" " << ss.str();
cout <<" " << s[0] << s[1] << s[2] << endl;

t1 = (unsigned long)GetCycleCount();
for (int i =0; i< 10000000; i++ ) {
a.ResetP();
a.write_8(ch);
a.write_16(sh);
a.write_32(in);
a.write_8(ch);
a.write_16(sh);
a.write_32(in);
}
t2 = (unsigned long)GetCycleCount();
cout << "用时: " << t2 - t1;
cout <<" " << ss.str();
cout <<" " << s[0] << s[1] << s[2] << endl;
t1 = (unsigned long)GetCycleCount();
for (int i =0; i< 10000000; i++ ) {
b.ResetP();
b.write_8(ch);
b.write_16(sh);
b.write_32(in);
b.write_8(ch);
b.write_16(sh);
b.write_32(in);
}
t2 = (unsigned long)GetCycleCount();
cout << "用时: " << t2 - t1;
cout <<" " << ss.str();
cout <<" " << s[0] << s[1] << s[2] << endl;

cin >> ch;
return 0;
}


 
  评论这张
 
阅读(547)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017