Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #37076 > unrolled thread

What practice can get speed improvement in C++?

Started byfl <rxjwg98@gmail.com>
First post2015-07-14 16:53 -0700
Last post2015-08-03 04:22 -0700
Articles 5 on this page of 45 — 17 participants

Back to article view | Back to comp.lang.c++


Contents

  What practice can get speed improvement in C++? fl <rxjwg98@gmail.com> - 2015-07-14 16:53 -0700
    Re: What practice can get speed improvement in C++? Barry Schwarz <schwarzb@dqel.com> - 2015-07-14 17:23 -0700
      Re: What practice can get speed improvement in C++? bartekltg <bartekltg@gmail.com> - 2015-07-15 15:10 +0200
        Re: What practice can get speed improvement in C++? JiiPee <no@notvalid.com> - 2015-07-16 05:00 +0100
          Re: What practice can get speed improvement in C++? Öö Tiib <ootiib@hot.ee> - 2015-07-16 01:04 -0700
            Re: What practice can get speed improvement in C++? JiiPee <no@notvalid.com> - 2015-07-16 16:48 +0100
              Re: What practice can get speed improvement in C++? JiiPee <no@notvalid.com> - 2015-07-16 16:52 +0100
          Re: What practice can get speed improvement in C++? bartekltg <bartekltg@gmail.com> - 2015-07-16 13:23 +0200
            Re: What practice can get speed improvement in C++? Öö Tiib <ootiib@hot.ee> - 2015-07-16 06:54 -0700
              Re: What practice can get speed improvement in C++? bartekltg <bartekltg@gmail.com> - 2015-07-16 17:30 +0200
                Re: What practice can get speed improvement in C++? Öö Tiib <ootiib@hot.ee> - 2015-07-16 11:16 -0700
                  Re: What practice can get speed improvement in C++? bartekltg <bartekltg@gmail.com> - 2015-07-17 17:06 +0200
            Re: What practice can get speed improvement in C++? JiiPee <no@notvalid.com> - 2015-08-04 16:14 +0100
    Re: What practice can get speed improvement in C++? Victor Bazarov <v.bazarov@comcast.invalid> - 2015-07-14 20:29 -0400
      Re: What practice can get speed improvement in C++? woodbrian77@gmail.com - 2015-07-14 19:50 -0700
    Re: What practice can get speed improvement in C++? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2015-07-15 05:07 +0200
    Re: What practice can get speed improvement in C++? Lőrinczy Zsigmond <nospam@for.me> - 2015-07-15 06:38 +0200
    Re: What practice can get speed improvement in C++? Paavo Helde <myfirstname@osa.pri.ee> - 2015-07-15 00:22 -0500
      Re: What practice can get speed improvement in C++? fl <rxjwg98@gmail.com> - 2015-07-16 04:34 -0700
        Re: What practice can get speed improvement in C++? Martijn van Buul <pino@dohd.org> - 2015-07-21 09:28 +0000
    Re: What practice can get speed improvement in C++? Juha Nieminen <nospam@thanks.invalid> - 2015-08-03 08:16 +0000
      Re: What practice can get speed improvement in C++? woodbrian77@gmail.com - 2015-08-03 13:48 -0700
        Re: What practice can get speed improvement in C++? Juha Nieminen <nospam@thanks.invalid> - 2015-08-04 08:21 +0000
          Re: What practice can get speed improvement in C++? Öö Tiib <ootiib@hot.ee> - 2015-08-04 08:07 -0700
          Re: What practice can get speed improvement in C++? woodbrian77@gmail.com - 2015-08-04 10:15 -0700
            Re: What practice can get speed improvement in C++? Bo Persson <bop@gmb.dk> - 2015-08-04 20:09 +0200
              Re: What practice can get speed improvement in C++? woodbrian77@gmail.com - 2015-08-04 12:38 -0700
                Re: What practice can get speed improvement in C++? Bo Persson <bop@gmb.dk> - 2015-08-05 17:03 +0200
                  Re: What practice can get speed improvement in C++? woodbrian77@gmail.com - 2015-08-05 23:32 -0700
              Re: What practice can get speed improvement in C++? Öö Tiib <ootiib@hot.ee> - 2015-08-05 03:55 -0700
            Re: What practice can get speed improvement in C++? Christopher Pisz <nospam@notanaddress.com> - 2015-08-04 14:09 -0500
              Re: What practice can get speed improvement in C++? woodbrian77@gmail.com - 2015-08-04 12:18 -0700
                Re: What practice can get speed improvement in C++? Christopher Pisz <nospam@notanaddress.com> - 2015-08-04 14:50 -0500
                Re: What practice can get speed improvement in C++? Öö Tiib <ootiib@hot.ee> - 2015-08-05 04:11 -0700
            Re: What practice can get speed improvement in C++? Juha Nieminen <nospam@thanks.invalid> - 2015-08-05 08:12 +0000
              Re: What practice can get speed improvement in C++? Martin Shobe <martin.shobe@yahoo.com> - 2015-08-05 08:53 -0500
              Re: What practice can get speed improvement in C++? woodbrian77@gmail.com - 2015-08-05 22:24 -0700
                Re: What practice can get speed improvement in C++? Ian Collins <ian-news@hotmail.com> - 2015-08-06 17:42 +1200
                  Re: What practice can get speed improvement in C++? woodbrian77@gmail.com - 2015-08-05 22:53 -0700
                    Re: What practice can get speed improvement in C++? Öö Tiib <ootiib@hot.ee> - 2015-08-06 00:20 -0700
                    Re: What practice can get speed improvement in C++? Christopher Pisz <nospam@notanaddress.com> - 2015-08-06 10:10 -0500
                  Re: What practice can get speed improvement in C++? Bo Persson <bop@gmb.dk> - 2015-08-06 17:29 +0200
                    Re: What practice can get speed improvement in C++? woodbrian77@gmail.com - 2015-08-06 09:38 -0700
                Re: What practice can get speed improvement in C++? Juha Nieminen <nospam@thanks.invalid> - 2015-08-10 08:25 +0000
    Re: What practice can get speed improvement in C++? pip010 <pip010@gmail.com> - 2015-08-03 04:22 -0700

Page 3 of 3 — ← Prev page 1 2 [3]


#37361

FromChristopher Pisz <nospam@notanaddress.com>
Date2015-08-06 10:10 -0500
Message-ID<mpvt9r$tk7$1@dont-email.me>
In reply to#37358
On 8/6/2015 12:53 AM, woodbrian77@gmail.com wrote:
> On Thursday, August 6, 2015 at 12:42:31 AM UTC-5, Ian Collins wrote:
>> woodbrian77@gmail.com wrote:
>>>
>>> How about comparing these two programs:
>>>
>>> #include <string.h>
>>>
>>> int main ()
>>> {
>>>     for (int i = 0; i < 4000000; ++i){
>>>       char s[200];
>>>       ::strncpy(s, "We few; we happy few; we band of brothers.", sizeof(s));
>>>     }
>>> }
>>>
>>> and
>>>
>>> #include <string>
>>>
>>> int main ()
>>> {
>>>     for (int i = 0; i < 4000000; ++i){
>>>       ::std::string s = "We few; we happy few; we band of brothers.";
>>>     }
>>> }
>>
>> A fairer comparison would be
>>
>> int main ()
>> {
>>     std::string s;
>>     for (int i = 0; i < 4000000; ++i){
>>       s = "We few; we happy few; we band of brothers.";
>>     }
>> }
>>
>
>
> Here's the time for that version.
>
> time ./stdstr
>
> real	0m0.120s
> user	0m0.118s
> sys	0m0.001s
>
> In most cases I can't lift a std::string out of the loop like that
> though.
>

A fairer comparison would be

int main ()
{
     std::string s;
     s.reserve(200);
     for (int i = 0; i < 4000000; ++i)
     {
         s = "We few; we happy few; we band of brothers.";
     }
}



-- 
I have chosen to troll filter/ignore all subthreads containing the
words: "Rick C. Hodgins", "Flibble", and "Islam"
So, I won't be able to see or respond to any such messages
---

[toc] | [prev] | [next] | [standalone]


#37363

FromBo Persson <bop@gmb.dk>
Date2015-08-06 17:29 +0200
Message-ID<d2hcqmFlp2cU1@mid.individual.net>
In reply to#37357
On 2015-08-06 07:42, Ian Collins wrote:
> woodbrian77@gmail.com wrote:
>>
>> How about comparing these two programs:
>>
>> #include <string.h>
>>
>> int main ()
>> {
>>    for (int i = 0; i < 4000000; ++i){
>>      char s[200];
>>      ::strncpy(s, "We few; we happy few; we band of brothers.",
>> sizeof(s));
>>    }
>> }
>>
>> and
>>
>> #include <string>
>>
>> int main ()
>> {
>>    for (int i = 0; i < 4000000; ++i){
>>      ::std::string s = "We few; we happy few; we band of brothers.";
>>    }
>> }
>
> A fairer comparison would be
>
> int main ()
> {
>    std::string s;
>    for (int i = 0; i < 4000000; ++i){
>      s = "We few; we happy few; we band of brothers.";
>    }
> }
>

Or try doing this with C strings:

int main ()
{
    std::string s;
    for (int i = 0; i < 4000000; ++i){
      s += "We few; we happy few; we band of brothers.";
    }
}



Bo Persson

[toc] | [prev] | [next] | [standalone]


#37364

Fromwoodbrian77@gmail.com
Date2015-08-06 09:38 -0700
Message-ID<f3ffc739-f4fd-428d-beb2-45a809b02027@googlegroups.com>
In reply to#37363
On Thursday, August 6, 2015 at 10:29:38 AM UTC-5, Bo Persson wrote:
> On 2015-08-06 07:42, Ian Collins wrote:
> 
> Or try doing this with C strings:
> 
> int main ()
> {
>     std::string s;
>     for (int i = 0; i < 4000000; ++i){
>       s += "We few; we happy few; we band of brothers.";
>     }
> }
> 


No thanks.  I don't want to totally stop using std::string, but 
I have some cases where I'm not appending anything to the initial 
value.  

Previously I was more comfortable using std::unique_ptr than
I am now.  I still use unique_ptr, but not as much as before.
This is similar with std::string.


Brian
Ebenezer Enterprises 
http://webEbenezer.net

[toc] | [prev] | [next] | [standalone]


#37400

FromJuha Nieminen <nospam@thanks.invalid>
Date2015-08-10 08:25 +0000
Message-ID<mq9n5v$2tao$1@adenine.netfront.net>
In reply to#37356
woodbrian77@gmail.com wrote:
>  for (int i = 0; i < 4000000; ++i){
>    char s[200];
>    ::strncpy(s, "We few; we happy few; we band of brothers.", sizeof(s));

You are copying data from a string literal to a stack-allocated static array.
Of course it's going to be faster than allocating a string dynamically.
(And of course it should be done like that, if it suffices and if it indeed
requires the speed.)

Btw, are you sure the compiler isn't optimizing the loop away? After all,
it can see that 's' isn't doing anything. It can also see that the body
of the loop isn't using the loop variable nor has any side effects.
I wouldn't be surprised if the complier optimized the whole thing away.

For a more reliable (and fairer) comparison,  change it so that the string
being built is of a length unknown at compile time, and the result is
used for something...

--- news://freenews.netfront.net/ - complaints: news@netfront.net ---

[toc] | [prev] | [next] | [standalone]


#37305

Frompip010 <pip010@gmail.com>
Date2015-08-03 04:22 -0700
Message-ID<586451a5-6256-43d9-b10a-c99bd79597fe@googlegroups.com>
In reply to#37076
On Wednesday, July 15, 2015 at 1:53:43 AM UTC+2, fl wrote:
> Hi,
> 
> I am given the following question:
> 
> 
> 
> Experience in C/C++ code optimization for speed improvement.
> 
> 
> 
> I can only get the following answers:
> 
> 1. Use reference to replace a direct parameter in function call;
> 2. Use register for some frequent variables;
> 3. Use assembly code to replace C code
> 4. Unroll a loop(?)
> 
> 
> What is your opinion to the question?
> 
> Thanks,

most comments are right on!
just my addition:
1) mostly useless, c++11 compiler will move so even pointless for BigInt
2) as rightfully some people already pointed out, forget about it.
3) hardly a portable solution and hard times getting it right left alone better than the compiler
4) it is done by the optimization step of compiler, I think at least O2 but see compiler docs; most modern compilers will unroll and/or vectorize for loops

Always think of improving your design first to jump on low-level optimization techniques.
Profiling can help alot but only in debug :(
to truly profile you need special build release+pdb and things progressively become more complicated.
You can always measure though :) DO SO!

[toc] | [prev] | [standalone]


Page 3 of 3 — ← Prev page 1 2 [3]

Back to top | Article view | comp.lang.c++


csiph-web