[C++]string.compare() vs strcmp()

결과


strA==strA


==> elapsed time : 1.08 sec

strA==strB


==> elapsed time : 1.09 sec

strcmp(szA,szB)


==> elapsed time : 0.12 sec

strcmp(szA,szA)


==> elapsed time : 0.11 sec

strcmp(strA,strB)


==> elapsed time : 0.25 sec

strcmp(strA,strA)


==> elapsed time : 0.25 sec

Source Code



#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <string>
#include “Blib.h”

using namespace std;
#define TEST_SIZE 1<<20
#define ITER 1<<25

void compare()
{
string strBuffer1(TEST_SIZE, ‘a’);
string strBuffer2(TEST_SIZE, ‘b’);

char szBuffer1[TEST_SIZE+1];
memset(szBuffer1 , ‘a’, TEST_SIZE);
szBuffer1[TEST_SIZE]=’\0′;
char szBuffer2[TEST_SIZE+1];
memset(szBuffer2 , ‘b’, TEST_SIZE);
szBuffer2[TEST_SIZE]=’\0′;

Blib::TStopWatch w;
puts(“strA==strA\n”);
for (int i=0;i<ITER;i++)
strBuffer1 == strBuffer2;
w.Print(stderr);

w.Reset();
puts(“strA==strB\n”);
for (int i=0;i<ITER;i++)
strBuffer1 == strBuffer2;
w.Print(stderr);

w.Reset();
puts(“strcmp(szA,szB)\n”);
for (int i=0;i<ITER;i++)
strcmp(szBuffer1, szBuffer2);
w.Print(stderr);

w.Reset();
puts(“strcmp(szA,szA)\n”);
for (int i=0;i<ITER;i++)
strcmp(szBuffer1, szBuffer1);
w.Print(stderr);

w.Reset();
puts(“strcmp(strA,strB)\n”);
for (int i=0;i<ITER;i++)
strcmp(strBuffer1.c_str(), strBuffer2.c_str());
w.Print(stderr);

w.Reset();
puts(“strcmp(strA,strA)\n”);
for (int i=0;i<ITER;i++)
strcmp(strBuffer1.c_str(), strBuffer1.c_str());
w.Print(stderr);
}

main()
{
compare();
}


분석


어이없다;;; string 안에 뭐가 들어있길래 저리 차이가 나누???