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


Groups > comp.lang.c++ > #37340

Re: What practice can get speed improvement in C++?

From Christopher Pisz <nospam@notanaddress.com>
Newsgroups comp.lang.c++
Subject Re: What practice can get speed improvement in C++?
Date 2015-08-04 14:50 -0500
Organization A noiseless patient Spider
Message-ID <mpr503$jei$1@dont-email.me> (permalink)
References (2 earlier) <bc4afec4-babe-46b1-83e4-fa3de87cfeb3@googlegroups.com> <mppslj$iam$1@adenine.netfront.net> <f76c748e-03ed-41ae-8e8c-6bd49b6f9f3c@googlegroups.com> <mpr2hf$83g$1@dont-email.me> <074fdee6-e44c-46d3-b65f-3ec8b02349db@googlegroups.com>

Show all headers | View raw


On 8/4/2015 2:18 PM, woodbrian77@gmail.com wrote:
> On Tuesday, August 4, 2015 at 2:09:19 PM UTC-5, Christopher Pisz wrote:
>> On 8/4/2015 12:15 PM, woodbrian77@gmail.com wrote:
>>> On Tuesday, August 4, 2015 at 3:21:29 AM UTC-5, Juha Nieminen wrote:
>>>> woodbrian77@gmail.com wrote:
>>>>> I think there's a place for std::string, but I use it less
>>>>> than previously.  I guess it's back to the future with arrays
>>>>> of char.
>>>>
>>>> How would an array of chars be faster than std::string? Unless you are
>>>> using a static array (or somehow reusing the same allocated array,
>>>> which becomes a bit complicated)?
>>>>
>>>
>>> This is interesting
>>>
>>> http://stackoverflow.com/questions/21946447/how-much-performance-difference-when-using-string-vs-char-array
>>>
>>>> (Also note that the major compiler implementations are moving to
>>>> short string optimization, which means that if the majority of your
>>>> strings are short, then std::string will probably be faster than
>>>> using raw C strings.)
>>>>
>>>
>>> I think that's limited to about 20 characters.
>>>
>>> Brian
>>> Ebenezer Enterprises - "America didn't create religious liberty;
>>> religious liberty created America."  Bobby Jindal
>>>
>>> http://webEbenezer.net
>>>
>>
>>
>> Like all the flawed comparisons, notice they never reserve even though
>> the length is known beforehand.
>>
>>
>
> He makes a comment about that on one of the answers:
>    	
> "I even tried to pre-declare and allocate space in the string in my answer, but that actually just caused things to slow down."
>
> Brian
> Ebenezer Enterprises - In G-d we trust.
> http://webEbenezer.net
>

I had no such trouble:


// Shared Library
#include "PerformanceTimer.h"

// Standard Library
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>

using namespace std;
#define TRIALS 10000000


int main()
{

     vector<double> secondsElapsedPerRun;

     //const char * baseLocation = "baseLocation";
     const string baseLocation = "baseLocation";

     // Let's declare them both before hand rather than just the C version
     // Let's also use std::string::reserve just like we supply the 
length of the c-array
     char fname[255] = {};
     //string fname;
     //fname.reserve(255);

     Shared::PerformanceTimer timer;
     for (int i = 0; i < TRIALS; ++i)
     {
         timer.Start();

         _snprintf_s(fname, 255, "%s_test_no.%d.txt", 
baseLocation.c_str(), i);
         //fname = baseLocation + "_test_no." + std::to_string(i) + ".txt";

         secondsElapsedPerRun.push_back(timer.Stop());
     }

     // Calculate sum
     double sum = 0.0;

     for(vector<double>::iterator vecIter = 
secondsElapsedPerRun.begin(); vecIter != secondsElapsedPerRun.end(); 
++vecIter)
     {
         sum+= *vecIter;
     }

     double average = sum / static_cast<double>(TRIALS);
     cout << "Average: " << average << " seconds" << endl;

     // Calculate variance
     double variance = 0;

     for(vector<double>::iterator vecIter = 
secondsElapsedPerRun.begin(); vecIter != secondsElapsedPerRun.end(); 
++vecIter)
     {
         variance += (*vecIter - average) * (*vecIter - average);
     }

     variance /= static_cast<double>(TRIALS);

     cout << "Variance: " << variance << " seconds" << endl;
     cout << "Std. deviation: " << sqrt(variance) << " seconds" << endl;

     // Windows specific
     system("pause");
}




C++ string
Average: 1.21634e-006 seconds
Variance: 5.56171e-013 seconds
Std. deviation: 7.45769e-007 seconds

C string
Average: 6.3834e-007 seconds
Variance: 4.31959e-012 seconds
Std. deviation: 2.07836e-006 seconds

Seems a bit more fair.


-- 
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
---

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web