char str[1024] = {0}比 char str[1024];memset(str,0,1024)效率高

wangchentangjuan 2013-04-15 02:52:50
我看书上说char str[1024];memset(str,0,1024)这种效率高,因为在64位机器下,可能是8位一置0的,但是我自己写了个测试代码
#include<windows.h>
#include <winbase.h>

int istart= GetTickCount();
for (int i = 0 ; i < 1000000 ;++i)
{

char my[256];
memset(my,0,256);

}

int iend= GetTickCount();
int pos = iend - istart;


istart= GetTickCount();
for (int i = 0 ; i < 1000000 ;++i)
{

char my[256] = {0};

}
iend= GetTickCount();
pos = iend - istart;


后者比前者快了很多,测试环境 vs2005 vs2012 (WIN7)、64位win8 VS2012
求大神指导
...全文
316 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
pathuang68 2013-04-15
  • 打赏
  • 举报
回复
我用楼主的代码(见下面)在VS2010上得到的结果是memset快。

#include <iostream>
#include <time.h>
using namespace std;

int main(int argc, char** argv)
{
	int istart= clock();
	for (int i = 0 ; i < 1000000 ;++i)
	{
		char my[256];
		memset(my,0,256);
	}
 
	int iend= clock();
	int pos = iend - istart;
	cout << pos << endl;                        // 输出15
 

	istart= clock();
	for (int i = 0 ; i < 1000000 ;++i)
	{
		char my[256] = {0};
	}
	iend= clock();
	pos = iend - istart;
	cout << pos << endl;                        // 输出:31

	return 0;
}
starytx 2013-04-15
  • 打赏
  • 举报
回复
我的vs2010+32位win7怎么感觉前者快啊,前者有时候还是pos还是0,而后者没有0的时候

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧