C + + stitching string efficiency comparison (+ =, append, StringStream, sprintf)

Source: Internet
Author: User
Tags sprintf

Turn from: http://www.cnblogs.com/james6176/p/3222671.htmlc++ stitching string Efficiency comparison (+ =, append, StringStream, sprintf)

Recently wrote the program with a large number of splicing strings, in order to improve the efficiency of splicing, compared to + =, append, StringStream, sprintf four kinds of splicing string method.

Test method

The comparison method is to write 4 functions, respectively, with + =, append, StringStream, sprintf the way to stitch the string, stitching method is s1= "ABCEDFG", s2= "HIJKLMN", s3= "Opqrst" three strings are stitched together, The total loop is 60 times. The 4 functions are then called sequentially in the main function and timed with a timestamp. In order to make the time difference more obvious, we can take the time of cyclic n (n can be 100 or 1000000) times. The code is as follows:

1 #include <iostream>2 #include <String>3 #include <sys/time.h>4 #include <sstream>5 #include <stdio.h>6UsingNamespaceStd7#define OUT_IN_REPEATE_NUM 100008#define IN_REPEATE_NUM 60910String s1="Abcedfg";11String s2="Hijklmn";12String s3="Opqrst";13void Plustest (string&Ret14{15Forint i=0; i<in_repeate_num; i++)16{+ ret + =S1;RET + =S2;RET + =S3;20}21st}22void Appendtest (string&Ret23{24Forint i=0; i<in_repeate_num; i++)25{26Ret.append (S1);27Ret.append (S2);28Ret.append (S3);29}30}31void Sprintftest (string&Ret32{33Const size_t Length=26*In_repeate_num;34CharTmp[length];35char* CP =tmpsize_t strlength=s1.length () +s2.length () +S3.length ();37Forint i=0; i<in_repeate_num; i++)38{sprintf (CP,"%s%s%s", S1.c_str (), S2.c_str (), S3.c_str ());cp+=Strlength;41}RET =tmp43}4445void Sstest (string&Ret46{47StringStream SS;48Forint i=0; i<in_repeate_num; i++)49{ss<<S1;Wuyi ss<<S2;ss<<S3;53}The RET =Ss.str ();55}56IntMain () {57StringSS, plus, append, sprintf;58structTimeval stime, ETime;59Gettimeofday (&Stime, NULL);61Forint i=0; i<out_in_repeate_num; i++)62{sprintf="";64Sprintftest (sprintf);65}Gettimeofday (&ETime, NULL);67Long sprintftime = (etime.tv_sec-stime.tv_sec) *1000000+ (ETIME.TV_USEC-STIME.TV_USEC);//Exetime units are microseconds68Gettimeofday (&Stime, NULL);70Forint i=0; i<out_in_repeate_num; i++)71{Append="";73Appendtest (append);74} Gettimeofday (& ETime, NULL); Long appendtime = (etime.tv_sec-stime.tv_sec) *1000000+ (ETIME.TV_USEC-STIME.TV_USEC); Exetime Unit is microsecond gettimeofday (&stime, NULL); for (int i=0; i<out_in_repeate_num; i++), {Bayi ss= "", "the sstest (ss); (Gettimeofday, NULL); Sstime = (etime.tv_sec-stime.tv_sec) *1000000+ (ETIME.TV_USEC-STIME.TV_USEC); Exetime Unit is microsecond gettimeofday (&stime, NULL); (int i=0; i<out_in_repeate_num; i++), Plustest (Gettimeofday, NULL); plus= &etime (plus); 94 Long Plustime = (etime.tv_sec-stime.tv_sec) *1000000+ (ETIME.TV_USEC-STIME.TV_USEC); Exetime Unit is microsecond cout<< "Plustime is:" <<PlusTime<<endl; cout<< "Appendtime is:" <<AppendTime<<endl; 98 cout<< "Sstime is:" <<SsTime<<endl; cout<< "Sprintftime is:" <<sprintftime<<endl;100 if (ss==sprintf && Append==plus & & Ss==plus) 101 {102 cout<< "They is same" <<endl;103 }104 else105 {106 cout<< "different!" <<endl;107 cout<< "Sprintf:" <<sprintf<<endl;108 cout<< "ss:" <<ss<<endl; 109 cout<< "Plus:" <<plus<<endl;110 cout<< "Append:" <<append<<endl;111}112 113}

Test Results:

In the Linux environment, compile the above code with g++, and the result is as follows (the time unit is μm):

Outer loop 1000000 times

Outer loop 100000 times

Outer Loop 10000 times

Outer Loop of three times

Outer Loop of three times

Plustime is:3405450

Appendtime is:4020078

Sstime is:7835499

Sprintftime is:14875433

They is same

Plustime is:337229

Appendtime is:401719

Sstime is:788242

Sprintftime is:1517999

They is same

Plustime is:32177

Appendtime is:40265

Sstime is:78928

Sprintftime is:150839

They is same

Plustime is:3402

Appendtime is:4074

Sstime is:7984

Sprintftime is:15425

They is same

Plustime is:369

Appendtime is:429

Sstime is:921

Sprintftime is:1591

They is same

Conclusion:

Based on the above results, if you are using a Linux system and are g++ (GCC) compilers, the efficiency of a large number of stitching strings is from high to low: + =, append (), StringStream, sprintf ().

-Rookie Jim Original, if there is a mistake, please correct me!

C + + stitching string efficiency comparison (+ =, append, StringStream, sprintf)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.