码迷,mamicode.com
首页 > 其他好文 > 详细

strcmp和==比较

时间:2016-01-06 11:55:38      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

#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;
}

技术分享

 

去掉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;
}

技术分享

 

windows里

LARGE_INTEGER

QueryPerformanceCounter

QueryPerformanceFrequency

timeGettime

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

strcmp和==比较

标签:

原文地址:http://www.cnblogs.com/panyingying/p/5104878.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!