Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++

Name: Anon 2009-01-28 2:07

Hey, I am a complete noob to c++, and I need to know how to add strings.

Eg, I have:
a="one"
b="two"
c="three"

and I would like "d" to equal "onetwothree".

Thank you for any help

Name: Anonymous 2009-01-29 11:29

>>27
I concede that.

With gcc 4.2.1 on my machine, the common subexpressions in >>5 are actually optimized out when -O2 is used:

>>5 took 223 ms
>>11 took 927 ms
>>14 took 236 ms


Declaring the lengths as const rather than volatile allows gcc to make the same optimization, I suspect --

--- test.c.orig 2009-01-29 11:17:03.000000000 -0500
+++ test.c      2009-01-29 11:15:37.000000000 -0500
@@ -44,11 +44,10 @@
 
     then=NOW();
     for(i=0;i<HOWMANY;i++){
-        volatile unsigned int la,lb,lc;
-      
-        la=strlen(a);
-        lb=strlen(b);
-        lc=strlen(c);
+        const unsigned int
+                       la=strlen(a),
+               lb=strlen(b),
+               lc=strlen(c);
       
         d=malloc(la+lb+lc+1);

>>5 took 223 ms
>>11 took 930 ms
>>14 took 224 ms


Interestingly enough, with gcc 3.4.6, volatile produces faster code than const (though significantly slower than 4.2):

volatile:
>>5 took 1141 ms
>>11 took 1309 ms
>>14 took 746 ms

const:
>>5 took 1135 ms
>>11 took 1309 ms
>>14 took 766 ms


The signficant difference between the gcc 4.2 and 3.4 output is because the builtins for string manipulations weren't introduced until the 4.x series. This can be demonstrated by disabling builtins --

gcc34 -fno-builtin -O2 test.c
>>5 took 1446 ms
>>11 took 1434 ms
>>14 took 834 ms

gcc -fno-builtin -O2 test.c
>>5 took 1471 ms
>>11 took 1451 ms
>>14 took 896 ms


Anyway, enough of that. Let's all get back to Haskell and pretend this thread was never posted.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List