// test.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include#include #include int _tmain(int argc, _TCHAR* argv[]){ int size = 999999; TCHAR sCfgFile[MAX_PATH]; DWORD dw_ret = GetModuleFileName(NULL, sCfgFile, MAX_PATH); time_t start, end ; time(&start); for (int i = 0; i < size; ++i) { std::wstring path = sCfgFile; if (0 != dw_ret) { size_t pos = path.find_last_of('\\'); if (-1 != pos) { path = path.substr(0, pos + 1); } } } time(&end); double cost=difftime(end,start); //wprintf(L"转化成字符串后,获取路径话费时间----%f\n",cost); printf("转化成字符串后,获取路径话费时间----%f\n",cost); time_t start1, end1 ; time(&start1); int i = 0; for (int j = 0; j < size; ++j) { if (dw_ret > 0) { for (i = lstrlen(sCfgFile); sCfgFile[i] != '\\' && i >0; i--); sCfgFile[i + 1] = '\0'; } } time(&end1); double cost1=difftime(end1,start1); //wprintf(L"for循环找\字符获取路径----%f\n",cost1); printf("for循环找字符获取路径----%f\n",cost1); time_t start2, end2 ; time(&start2); for (int i = 0; i < size; ++i) { } time(&end2); double cost2=difftime(end2,start2); printf("空循环----%f\n",cost2); system("pause"); return 0;}
这里是unicode编译环境
如果是多字节编译的话,可以将
lstrlen(sCfgFile)换成strlen(sCfgFile), 将sCfgFile的类型换成 char 这里得到的结果是