|
네.. 실질적으로 char 보다 빠르지는 않습니다.
(몇몇 문자열 치환이나 이런 알고리즘성을 제외한 경우입니다)
만약, String을 이용하시고 조금 더 빠른 속도를 내시려면
String의 .c_str() 을 직접 메모리 억세스 하시면 될것 같습니다.
메모리 없는 정보에 쓰지 않도록 그런 쪽에만 유의해 주시고.. (setlength등의 사용)
char * 형으로 c_str() 정보를 받아서 계속되는 형변환 속도를 막아주셔서
받아온 char *형에 보통의 char *형 억세스 하듯이 억세스 하시면 될것 같습니다.
아래 예제는..
button1 은 char형으로만.
button2 는 ansistring으로만
button3 은 ansistring을 char형으로 접근을 테스트 한 것입니다.
제 컴퓨터의 경우에는 1, 3번은 평균 0.15초, 2번은 1.2초가 걸립니다.
cuperido
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int itStartTime = GetTickCount();
int itCount;
char *caString;
caString = (char *)malloc(100);
for(itCount = 0; itCount < 1000000; itCount ++) {
strcpy(caString, "안녕하세요. 반갑습니다");
}
free(caString);
ShowMessage(GetTickCount() - itStartTime);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
int itStartTime = GetTickCount();
int itCount;
String stString;
for(itCount = 0; itCount < 1000000; itCount ++) {
stString = "안녕하세요. 반갑습니다";
}
ShowMessage(GetTickCount() - itStartTime);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
int itStartTime = GetTickCount();
int itCount;
String stString;
char *caString;
stString.SetLength(strlen("안녕하세요. 반갑습니다"));
for(itCount = 0; itCount < 1000000; itCount ++) {
caString = stString.c_str();
strcpy(caString, "안녕하세요! 반갑습니다");
}
ShowMessage(GetTickCount() - itStartTime);
}
//---------------------------------------------------------------------------
DoyongID 님이 쓰신 글 :
: 서버를 짜는데, 일부 문자열 처리를 String으로 했습니다.
:
: 깔끔하고 보기도 좋아서 말이죠.. --; 근데, String으로 하면 많이 느립니까? 물론, 더 빠르지는 않을 것 같습니다만..
|