• 获取exe所在目录路径,速度


    // test.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <Windows.h>
    #include <time.h>
    #include <string>
    
    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
    ",cost);  
        printf("转化成字符串后,获取路径话费时间----%f
    ",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] = '';
            }
    
        }
    
        time(&end1);  
        double cost1=difftime(end1,start1);  
        //wprintf(L"for循环找字符获取路径----%f
    ",cost1);  
        printf("for循环找字符获取路径----%f
    ",cost1);  
    
        time_t start2, end2 ;   
        time(&start2);  
    
        for (int i = 0; i < size; ++i)
        {
        }
    
        time(&end2);  
        double cost2=difftime(end2,start2);  
        printf("空循环----%f
    ",cost2); 
    
        system("pause");
    
        return 0;
    }

    这里是unicode编译环境

    如果是多字节编译的话,可以将 

    lstrlen(sCfgFile)换成strlen(sCfgFile),
    将sCfgFile的类型换成 char

    这里得到的结果是

    
    
  • 相关阅读:
    两个数组的交集
    左叶子之和
    下载安装python
    占位
    2020 软件工程实践 助教总结
    安装使用 QEMU-KVM 虚拟化环境(Arch Linux / Manjaro / CentOS / Ubuntu )
    #69. 新年的QAQ
    1097E. Egor and an RPG game(Dilworth定理)
    #553. 【UNR #4】己酸集合
    #2099. 「CQOI2015」标识设计(插头dp)
  • 原文地址:https://www.cnblogs.com/XiHua/p/5085051.html
Copyright © 2020-2023  润新知