strcmp和==比较

本文由博主(YinaPan)原创,转载请注明出处:http://www.cnblogs.com/YinaPan/p/cpp_strcmp.html 
#include <stdio.h> #include <tchar.h> #include <iostream> #include <string> #include <Windows.h> using namespace std; #pragma comment(lib, "winmm.lib") int StringCompare(){ string strSource = "hello world!"; string strDest = "I am a luck dog!"; LARGE_INTEGER m_liPerfFreq = { 0 }; QueryPerformanceFrequency(&m_liPerfFreq); LARGE_INTEGER m_liPerfStart = { 0 }; QueryPerformanceCounter(&m_liPerfStart); for (int i = 0; i < 1e8; ++i) strSource == strDest; LARGE_INTEGER liPerfNow = { 0 }; QueryPerformanceCounter(&liPerfNow); int time = (((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * 1000) / m_liPerfFreq.QuadPart); cout << time << endl; return 0; } int CharCompare(){ char str1[100] = "hello world!"; char str2[100] = "I am a luck dog!"; LARGE_INTEGER m_liPerfFreq = { 0 }; QueryPerformanceFrequency(&m_liPerfFreq); LARGE_INTEGER m_liPerfStart = { 0 }; QueryPerformanceCounter(&m_liPerfStart); for (int i = 0; i < 1e8; ++i) strcmp(str1, str2); LARGE_INTEGER liPerfNow = { 0 }; QueryPerformanceCounter(&liPerfNow); int time = (((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * 1000) / m_liPerfFreq.QuadPart);//单位精确到毫秒,要是不乘以1000则就是秒 cout << time << endl; return 0; } int _tmain(int argc, _TCHAR* argv[]){ StringCompare(); CharCompare(); return 0; }

image

 

去掉10的8次方for循环的影响

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <Windows.h>

using namespace std;
#pragma comment(lib, "winmm.lib")

int StringCompare(){

    string strSource = "hello world!";
    string strDest = "I am a luck dog!";

    LARGE_INTEGER m_liPerfFreq = { 0 };
    QueryPerformanceFrequency(&m_liPerfFreq);

    LARGE_INTEGER m_liPerfStart = { 0 };
    QueryPerformanceCounter(&m_liPerfStart);

    for (int i = 0; i < 1e8; ++i)
        ;//strSource == strDest;

    LARGE_INTEGER liPerfNow = { 0 };
    QueryPerformanceCounter(&liPerfNow);

    int time = (((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * 1000) / m_liPerfFreq.QuadPart);
    cout << time << endl;

    return 0;
}

int CharCompare(){

    char str1[100] = "hello world!";
    char str2[100] = "I am a luck dog!";

    LARGE_INTEGER m_liPerfFreq = { 0 };
    QueryPerformanceFrequency(&m_liPerfFreq);

    LARGE_INTEGER m_liPerfStart = { 0 };
    QueryPerformanceCounter(&m_liPerfStart);

    for (int i = 0; i < 1e8; ++i)
        ;//strcmp(str1,str2);

    LARGE_INTEGER liPerfNow = { 0 };
    QueryPerformanceCounter(&liPerfNow);

    int time = (((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * 1000) / m_liPerfFreq.QuadPart);//单位精确到毫秒,要是不乘以1000则就是秒
    cout << time << endl;

    return 0;
}

int _tmain(int argc, _TCHAR* argv[]){

    StringCompare();
    CharCompare();

    return 0;
}

image

 

windows里

LARGE_INTEGER

QueryPerformanceCounter

QueryPerformanceFrequency

timeGettime

综上所述: 当达到10的8次方这个数量级时,string的相等操作确实是要比strcmp耗时,虽然string的重载的相等操作是memcmp内存的比较,但是之前还是有很多的判断的;

posted @ 2016-01-06 11:18  YinaPan  阅读(758)  评论(0编辑  收藏  举报