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


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

Re: C++ may sometimes be *too* simple (to use)

From Paavo Helde <myfirstname@osa.pri.ee>
Newsgroups comp.lang.c++
Subject Re: C++ may sometimes be *too* simple (to use)
Date 2021-04-27 17:57 +0300
Organization A noiseless patient Spider
Message-ID <s698nu$4tf$1@dont-email.me> (permalink)
References <s68upq$11to$1@gioia.aioe.org>

Show all headers | View raw


27.04.2021 15:07 Juha Nieminen kirjutas:
> Recently I posted a controversial thread where I recounted my recent
> experience trying to tutor a beginner programmer who wanted to create
> a small program that would read a file and allow the user to edit it,
> like a simple text editor, and how frustrating it was trying to do
> this in C due to the complexities of its memory management.
> 
> Because such accusations about C is heresy, I thought I would redeem
> myself and cleanse my sins by commenting a bit on the flipside, based
> on actual examples out there that I have had to deal with.
> 
> While trying to tutor that beginner, I was constantly thinking how
> much easier the task would have been in C++, using std::string
> (and std::vector, etc).
> 
> Indeed, std::string makes such things so much easier and simpler.
> On the flipside, perhaps it makes things a bit *too* easy, as it
> induces people to create inefficient code with it, perhaps due to
> ignorance or laziness.
> 
> For example, I am currently dealing with C++ code that does a lot of
> things like:
> 
>      if(str1.substr(n, 5) == str2)
> 
> Mind you, this is in commercial production code done by a relatively
> experienced C++ programmer, not some noob.
> 
> I'm constantly thinking like "sheesh, there's already a function that
> does that exact comparison in-place, without creating any temporary
> copies, avoiding a useless allocation, copying of data and deallocation."
> Namely:
> 
>      if(str1.compare(n, 5, str2) == 0)

I have wondered about this exact thing myself. So I now made a little 
test to check if and how much the slowdown actually is.

It looks like with small strings so that SSO gets used, substr() 
comparing is ca 2x slower than compare(), and with large strings 
(needing a dynamic allocation) substr() comparing is ca 10x slower than 
compare().

OTOH, a single compare() is only something like 4 ns, so if a program 
makes e.g. a million of such comparisons, then with large substrings it 
would slow down from 0.004 s to 0.04 s. I bet many people won't be 
overly concerned about this.


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


Thread

C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-04-27 12:07 +0000
  Re: C++ may sometimes be *too* simple (to use) Paavo Helde <myfirstname@osa.pri.ee> - 2021-04-27 17:57 +0300
    Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-04-27 18:14 -0700
      Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-04-28 05:21 +0000
        Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-04-28 09:09 -0700
          Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-04-29 04:45 +0000
            Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-04-29 15:16 -0700
              Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-04-30 10:10 +0000
                Re: C++ may sometimes be *too* simple (to use) Bo Persson <bo@bo-persson.se> - 2021-04-30 15:33 +0200
                Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-04 10:32 +0000
                Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-01 06:51 -0700
                Re: C++ may sometimes be *too* simple (to use) Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-03 21:38 +0300
                Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-06 18:21 -0700
                Re: C++ may sometimes be *too* simple (to use) Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-07 12:05 +0300
                Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-04 10:43 +0000
                Re: C++ may sometimes be *too* simple (to use) David Brown <david.brown@hesbynett.no> - 2021-05-04 15:20 +0200
                Re: C++ may sometimes be *too* simple (to use) Manfred <noname@add.invalid> - 2021-05-04 16:41 +0200
                Re: C++ may sometimes be *too* simple (to use) James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-05-04 23:42 -0400
                Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-05 05:01 +0000
                Re: C++ may sometimes be *too* simple (to use) David Brown <david.brown@hesbynett.no> - 2021-05-05 11:26 +0200
                Re: C++ may sometimes be *too* simple (to use) James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-05-05 09:53 -0400
                Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-06 21:40 -0700
                Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-07 07:38 +0000
                Re: C++ may sometimes be *too* simple (to use) Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-07 22:32 +0300
                Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-08 03:27 -0700
                Re: C++ may sometimes be *too* simple (to use) Paavo Helde <myfirstname@osa.pri.ee> - 2021-05-07 12:10 +0300
                Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-07 09:40 -0700
                Re: C++ may sometimes be *too* simple (to use) red floyd <no.spam.here@its.invalid> - 2021-05-07 17:59 -0700
                Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-08 03:42 -0700
                Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-08 20:27 +0000
                Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-09 09:56 -0700
                Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-10 05:07 +0000
                Re: C++ may sometimes be *too* simple (to use) Richard Damon <Richard@Damon-Family.org> - 2021-05-10 07:29 -0400
                Re: C++ may sometimes be *too* simple (to use) Öö Tiib <ootiib@hot.ee> - 2021-05-10 20:18 -0700
                Re: C++ may sometimes be *too* simple (to use) Hans Bos <hans.bos@xelion.nl> - 2021-05-07 16:06 +0200
  Re: C++ may sometimes be *too* simple (to use) Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-04-27 19:57 -0700
  Re: C++ may sometimes be *too* simple (to use) wij <wyniijj@gmail.com> - 2021-04-27 21:15 -0700
    Re: C++ may sometimes be *too* simple (to use) Paavo Helde <myfirstname@osa.pri.ee> - 2021-04-28 11:13 +0300
  Re: C++ may sometimes be *too* simple (to use) "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2021-05-03 10:28 +0200
    Re: C++ may sometimes be *too* simple (to use) Juha Nieminen <nospam@thanks.invalid> - 2021-05-04 10:53 +0000

csiph-web