本文由博主(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 = { };
QueryPerformanceFrequency(&m_liPerfFreq); LARGE_INTEGER m_liPerfStart = { };
QueryPerformanceCounter(&m_liPerfStart); for (int i = ; i < 1e8; ++i)
strSource == strDest; LARGE_INTEGER liPerfNow = { };
QueryPerformanceCounter(&liPerfNow); int time = (((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * ) / m_liPerfFreq.QuadPart);
cout << time << endl; return ; } int CharCompare(){ char str1[] = "hello world!";
char str2[] = "I am a luck dog!"; LARGE_INTEGER m_liPerfFreq = { };
QueryPerformanceFrequency(&m_liPerfFreq); LARGE_INTEGER m_liPerfStart = { };
QueryPerformanceCounter(&m_liPerfStart); for (int i = ; i < 1e8; ++i)
strcmp(str1, str2); LARGE_INTEGER liPerfNow = { };
QueryPerformanceCounter(&liPerfNow); int time = (((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * ) / m_liPerfFreq.QuadPart);//单位精确到毫秒,要是不乘以1000则就是秒
cout << time << endl; return ;
} int _tmain(int argc, _TCHAR* argv[]){ StringCompare();
CharCompare();
return ;
}

去掉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 = { };
QueryPerformanceFrequency(&m_liPerfFreq); LARGE_INTEGER m_liPerfStart = { };
QueryPerformanceCounter(&m_liPerfStart); for (int i = ; i < 1e8; ++i)
;//strSource == strDest; LARGE_INTEGER liPerfNow = { };
QueryPerformanceCounter(&liPerfNow); int time = (((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * ) / m_liPerfFreq.QuadPart);
cout << time << endl; return ;
} int CharCompare(){ char str1[] = "hello world!";
char str2[] = "I am a luck dog!"; LARGE_INTEGER m_liPerfFreq = { };
QueryPerformanceFrequency(&m_liPerfFreq); LARGE_INTEGER m_liPerfStart = { };
QueryPerformanceCounter(&m_liPerfStart); for (int i = ; i < 1e8; ++i)
;//strcmp(str1,str2); LARGE_INTEGER liPerfNow = { };
QueryPerformanceCounter(&liPerfNow); int time = (((liPerfNow.QuadPart - m_liPerfStart.QuadPart) * ) / m_liPerfFreq.QuadPart);//单位精确到毫秒,要是不乘以1000则就是秒
cout << time << endl; return ;
} int _tmain(int argc, _TCHAR* argv[]){ StringCompare();
CharCompare(); return ;
}

windows里

LARGE_INTEGER

QueryPerformanceCounter

QueryPerformanceFrequency

timeGettime

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

strcmp和==比较的更多相关文章

  1. Uva 11732 strcmp() Anyone?

    strcmp() Anyone? Time Limit: 2000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Subm ...

  2. Linux C 字符串函数 strlen()、strcat()、strncat()、strcmp()、strncmp()、strcpy()、strncpy() 详解

      strlen(返回字符串长度) 表头文件 #include <string.h> 定义函数 size_t strlen(const char *s); 函数说明 strlen()用来计 ...

  3. strcmp

     C++ Code  123456789101112   int strcmp(const char *dest, const char *source) {     assert((NULL !=  ...

  4. strlen(); strcpy(); strcat(); strcmp() ---笔记

    指针小知识点: int a =10; int *p=&a; int *q=p;        //p中保存的是a的地址 int *q=p;       //将p的值赋给q 作用是让q也指向a ...

  5. Strcmp(字符串1,字符串2)函数 Sizeof && strlen() Substr(a,b)

    Strcmp(字符串1,字符串2)函数 { strcmp函数是比较两个字符串的大小,返回比较的结果.一般形式是:  i=strcmp(字符串,字符串); 其中,字符串1.字符串2均可为字符串常量或变量 ...

  6. strcmp()&&mb_ereg_replace()&&ereg()

    主要记录两个函数,一个是strcmp(),一个是mb_ereg_replace() strcmp() php 5.3 以后字符串和数组比较会返回0 测试代码: PHP <?php $passwo ...

  7. strcmp函数使用总结

    Action() { /********************************* * Author:旺仔 * object:strcmp * date:2015-12-09 * fuc:我输 ...

  8. strcmp函数的使用

    Action() { /*********************************   * Author:旺仔   * object:strcmp   * date:2015-12-09    ...

  9. c/c++面试题(3)strcat/strcmp/strlen/strcpy的实现

    1.编写一个函数实现strlen以及strcpy函数. strcpy函数. 后面的字符串拷贝到一个字符数组中,要求拷贝好的字符串在字符数组的首 地址,并且只拷贝到'\0'的位置.原型是 char* m ...

  10. strcmp传入nil导致崩溃

    现象:连接电脑可以正常启动程序,不连接电脑启动程序就崩溃. 崩溃信息: BSXPCMessage received error for message: Connection invalid HW k ...

随机推荐

  1. h.264 FMO

    在H.264之前的标准中,比如H.263,其比特流中的数据是按照一个宏块接一个宏块的方式排列的,一旦发生丢包,很多相邻宏块信息都会丢失,很难进行错误隐藏处理.在H.264中加入了一项新特性:把宏块在比 ...

  2. 重新定义malloc和free 防止内存泄漏

    1, 定义供应用程序使用的头文件//libmem.h#ifndef _LIBMEM_H_#define _LIBMEM_H_ //声明自定义malloc及free函数extern void *my_m ...

  3. android 自定义titlebar

    首先,修改标题栏的宽度和背景,在style.xml中添加: <style> <item name="android:background">@drawabl ...

  4. redis 源码分析

    参考: http://redisbook.readthedocs.org/en/latest/index.html http://www.databaseskill.com/3421161/ The ...

  5. 14.4.1 Buffer Pool

    14.4.1 Buffer Pool buffer pool 是一个主人的内存区域 InnoDB caches 表和index 数据. buffer pool 允许经常访问的数据直接从内存里处理,加快 ...

  6. VisualSVN Server仓库迁移到Linux(包含所有版本, 权限,用户信息)

    公司开发服务器从Windows换成CentOS,所以要把原服务都转移到Linux下,MySQL.SMB的迁移都很顺利,但是SVN的转移却遇到了些问题,花费了三天时间,走了不少弯路,现在总算解决了SVN ...

  7. Minimum Depth of Binary Tree ——LeetCode

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  8. windows 编程 —— 消息与参数(滚动条、键盘、鼠标)

    目录: 滚动条 键盘 鼠标 滚动条ScrollBar 发送消息:WM_VSCROLL和WM_HSCROLL 参数wParam:wParam消息参数被分为一个低字组和一个高字组.wParam的低字组是一 ...

  9. 鼠标事件之鼠标滑过事件MOUSEOVER

    来源地址:http://www.g2room.com/jquery/index.php?p=example%2Fevent%2Fmouseover.html&n=%E9%BC%A0%E6%A0 ...

  10. 淘宝数据库OceanBase SQL编译器部分 源代码阅读--Schema模式

    淘宝数据库OceanBase SQL编译器部分 源代码阅读--Schema模式 什么是Database,什么是Schema,什么是Table,什么是列,什么是行,什么是User?我们能够能够把Data ...