프로그램 수행속도 비교 테스트 질문

cbycby의 이미지

Efficient C++ 책을 보다가... 프로그램 수행속도 향상에 대한 것을 보고 예제코드를 만들어 보았습니다.

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

char* test_2;
std::string test_1;

class A
{
public:
    A(const std::string& a)
    {
        std::string str(a);
    }

    A(const char* a)
    {
        std::string str(a);
    }
};

int test(int x)
{
    A a(test_1);
    //A a(test_2);

    return (x+1);
}

int main()
{
    test_1 = "ddd";
    test_2 = "ddd";

    for (size_t i=0; i<10000000; i++)
    {
        test(i);
    }
}

이런 코드를 실행하였을때,
아래의 주석 부분을 번갈아 가면서 막았다가 풀었을때 수행속도를 비교하였습니다.

int test(int x)
{
    A a(test_1);
    //A a(test_2);

    return (x+1);
}

그 결과 시스템 마다 편차가 있겠지만.
const char* 의 경우가 속도가 더 빨리 나왔습니다.
여기서 string& 으로 넘겼을때 const char* 보다 느려지는 원인이 무엇인가라는 궁금증이 생깁니다.
물론, string template source code를 따라가면서 추적하면 가장 좋겠지만. 공력이 낮은지라 분석이 힘드네요...

제가 나름대로 생각했을때.
string& 와 const char* 두개의 인자로 받는 부분은 같은 비용이라고 보고(pointer size 만큼)
std::string 객체에 실제 할당될때. const char* 의 경우 내부의 strcpy 문에 의해서 바로 복사되고.
string& 의 경우 const char* operator()가 호출되어 인자로 넘겨진 string&에 대한 버퍼 포인터를 얻어낸다음 위의 경우와 마찬가지로 strcpy를 호출하지 않을까라는 생각을 해봅니다.

제가 생각한 시나리오가 맞나요?

고수님들의 답변을 기다립니다.

doldori의 이미지

string 클래스를 어떻게 구현하는가에 따라 천차만별일 수 있습니다.
표준에서는 클래스의 인터페이스와 몇몇 사항만을 정할 뿐 구현 방법에
대해서는 전혀 언급하지 않거든요.
참고로 저는 gcc 3.2로 해봤는데 정반대의 결과가 나왔습니다. 훑어봤더니
gcc에서는 복사생성자에서 같은 문자열을 공유할 수 있도록 되어 있더군요.
이러면 당연히 test_1의 결과가 훨씬 빠릅니다. const char* 생성자보다
약 3배 정도 빠르더군요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.