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


Groups > comp.lang.c > #56473

Re: faster than c

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: faster than c
Date 2015-01-19 08:24 -0800
Organization None to speak of
Message-ID <lnoapupwkt.fsf@nuthaus.mib.org> (permalink)
References (1 earlier) <slrnmbkoug.3ps.grahn+nntp@frailea.sa.invalid> <42a7365b-a1a2-4287-a17a-270c2f168097@googlegroups.com> <66abefe5-b011-4784-8753-618db4c51326@googlegroups.com> <4397205f-c6de-45b4-899b-21aa24e1d2d8@googlegroups.com> <m9ikl0$fv7$1@dont-email.me>

Show all headers | View raw


David Brown <david.brown@hesbynett.no> writes:
> On 17/01/15 21:32, Öö Tiib wrote:
>> Most C++ libraries are template libraries. Templates let compiler's to do
>> as lot of work compile time as is possible and so generate more optimal
>> binaries. With C function calls it is more difficult for compiler to apply
>> such optimizations. 
>
> And that is a reason why C++ code can sometimes be faster than roughly
> equivalent C code.
>
> Given that almost all well-written C code is also valid C++ code, and

True if you count "almost all" by lines of code.  Any C program that
calls malloc() and does not cast the result is not a valid C++ program
(assuming it assigns the result to a non-void pointer object).  That
doesn't affect your point, though.

> that you are usually using the same compiler for C and C++,

Not exactly.  C and C++ compilers at least have different front ends.  I
suppose you could have a compiler front end that parses based on a
combined grammar that recognizes both C and C++, and that interprets the
meaning of the code (or rejects it) based on the current language, but I
don't think that's the usual approach.  A C and C++ compiler can share
optimization and code generation phases (as they do in gcc, for
example), and does tend to imply similar generated code for similar
constructs.

                                                              any C code
> can also be used as C++ at the same speed.  (The one feature of C++ that
> can sometimes adversely affect optimisation is exceptions - you may want
> to turn them off in compiler options if that's the case.)
>
> So while you certainly /could/ write slower C++ code than a given C
> program, you don't have to - you can write C++ code that is at least as
> fast as the C code.
>
> The question then is if you can write C++ code that is /faster/ than C
> code.  C++ has metaprogramming features such as templates that can
> generate multiple optimised versions of code as a replacement for more
> generic code (taking a "void*" and "size_t" parameter) - such optimised
> functions will almost always be faster than C-style generic functions.
> A template-based linked list or search algorithm will beat a generic
> void* one.  To get the same performance with C, you need to write
> individual functions for each type you are going to use, or write ugly
> macros.

C's qsort() is a good example.  (It's also C++'s qsort(), but it's
probably not used much in C++.)  C's lack of type-generic programming
means that the comparison function has to be called via a function
pointer.  A C++ template can expand to code that calls the comparison
function directly, which presumably can lead to substantially faster
code if the comparison is simple.

It would be interesting to take a C library qsort() implementation and
rewrite it as a very large macro, and compare the performance.

[...]

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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


Thread

faster than c fir <profesor.fir@gmail.com> - 2015-01-10 13:11 -0800
  Re: faster than c Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-10 14:26 -0800
    Re: faster than c Chad <cdalten@gmail.com> - 2015-01-10 14:43 -0800
      Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-10 15:20 -0800
    Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-10 15:39 -0800
    Re: faster than c glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-01-11 09:23 +0000
      Re: faster than c asetofsymbols@gmail.com - 2015-01-11 02:17 -0800
      Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-11 06:00 -0800
      Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-12 06:46 -0800
        Re: faster than c Morris Dovey <mrdovey@iedu.com> - 2015-01-12 15:01 -0600
          Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-12 13:44 -0800
            Re: faster than c Morris Dovey <mrdovey@iedu.com> - 2015-01-12 16:59 -0600
              Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-12 15:25 -0800
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-13 04:56 -0800
        Re: faster than c glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-01-12 23:26 +0000
          Re: faster than c jt@toerring.de (Jens Thoms Toerring) - 2015-01-13 01:10 +0000
          Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-13 05:16 -0800
  faster than c Chad <cdalten@gmail.com> - 2015-01-10 14:42 -0800
  Re: faster than c Cooler_x0a <c00f3r@gmail.com> - 2015-01-10 15:51 -0800
  Re: faster than c Geoff <geoff@invalid.invalid> - 2015-01-11 00:21 -0800
    Re: faster than c Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-11 02:27 -0800
      Re: faster than c gamo <gamo@telecable.es> - 2015-01-11 12:17 +0100
    Re: faster than c David Brown <david.brown@hesbynett.no> - 2015-01-11 21:02 +0100
    Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-21 03:44 -0800
      Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-21 05:08 -0800
      Re: faster than c Richard Heathfield <invalid@see.sig.invalid> - 2015-01-21 13:53 +0000
        Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-21 06:03 -0800
          Re: faster than c David Brown <david.brown@hesbynett.no> - 2015-01-22 15:06 +0100
            Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-22 06:57 -0800
              Re: faster than c mark.bluemel@gmail.com - 2015-01-22 07:27 -0800
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-22 07:36 -0800
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-22 07:45 -0800
            Re: faster than c Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-24 22:36 -0700
  Re: faster than c David Brown <david.brown@hesbynett.no> - 2015-01-11 21:16 +0100
  Re: faster than c Tim Prince <tprince@computer.org> - 2015-01-11 15:42 -0500
  Re: faster than c Michael Angelo Ravera <maravera@prodigy.net> - 2015-01-14 12:25 -0800
  Re: faster than c Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-01-17 13:29 +0000
    Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-17 05:32 -0800
    Re: faster than c Melzzzzz <mel@zzzzz.com> - 2015-01-17 14:42 +0100
      Re: faster than c Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-01-17 19:29 +0000
        Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-17 11:55 -0800
    Re: faster than c "Osmium" <r124c4u102@comcast.net> - 2015-01-17 08:00 -0600
    Re: faster than c Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-17 10:15 -0800
      Re: faster than c wssimms@gmail.com - 2015-01-17 10:31 -0800
        Re: faster than c glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-01-17 19:10 +0000
          Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-17 11:21 -0800
        Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-17 11:13 -0800
        Re: faster than c Öö Tiib <ootiib@hot.ee> - 2015-01-17 12:32 -0800
          Re: faster than c David Brown <david.brown@hesbynett.no> - 2015-01-19 11:01 +0100
            Re: faster than c Keith Thompson <kst-u@mib.org> - 2015-01-19 08:24 -0800
              Re: faster than c David Brown <david.brown@hesbynett.no> - 2015-01-19 22:52 +0100
              Re: faster than c Robert Wessel <robertwessel2@yahoo.com> - 2015-01-20 00:04 -0600
            Re: faster than c Öö Tiib <ootiib@hot.ee> - 2015-01-19 10:12 -0800
              Re: faster than c David Brown <david.brown@hesbynett.no> - 2015-01-19 23:05 +0100
                Re: faster than c Öö Tiib <ootiib@hot.ee> - 2015-01-19 16:23 -0800
            Re: faster than c Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-24 09:32 -0700
              Re: faster than c David Brown <david.brown@hesbynett.no> - 2015-03-24 19:08 +0100
                Re: faster than c Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-25 12:31 -0700
                Re: faster than c Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-03-25 12:45 -0700
                Re: faster than c Keith Thompson <kst-u@mib.org> - 2015-03-25 12:58 -0700
                Re: faster than c David Brown <david.brown@hesbynett.no> - 2015-03-25 21:22 +0100
                Re: faster than c Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-25 16:30 -0700
                Re: faster than c Melzzzzz <mel@zzzzz.com> - 2015-03-26 00:50 +0100
                Re: faster than c Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-28 09:20 -0700
                Re: faster than c David Brown <david.brown@hesbynett.no> - 2015-03-26 00:55 +0100
                Re: faster than c Melzzzzz <mel@zzzzz.com> - 2015-03-26 01:06 +0100
                Re: faster than c David Brown <david.brown@hesbynett.no> - 2015-03-26 11:34 +0100
                Re: faster than c Keith Thompson <kst-u@mib.org> - 2015-03-25 20:29 -0700
                Re: faster than c Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-25 16:34 -0700
                Re: faster than c Ian Collins <ian-news@hotmail.com> - 2015-03-26 13:53 +1300
                Re: faster than c Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-28 15:08 -0700
                Re: faster than c Ian Collins <ian-news@hotmail.com> - 2015-03-29 11:28 +1300
                Re: faster than c Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-30 12:42 -0700
                Re: faster than c Kaz Kylheku <kaz@kylheku.com> - 2015-03-30 20:06 +0000
                Re: faster than c Ian Collins <ian-news@hotmail.com> - 2015-03-31 16:37 +1300
                Re: faster than c Kaz Kylheku <kaz@kylheku.com> - 2015-03-31 05:13 +0000
                Re: faster than c Ian Collins <ian-news@hotmail.com> - 2015-03-31 16:33 +1300
                Re: faster than c Philip Lantz <prl@canterey.us> - 2015-03-31 00:14 -0700
      Re: faster than c wssimms@gmail.com - 2015-01-17 10:33 -0800
        Re: faster than c Greg Martin <greg.at.softsprocket@dot.com> - 2015-01-17 20:03 +0000
          Re: faster than c David Brown <david.brown@hesbynett.no> - 2015-01-19 11:07 +0100
          Re: faster than c wpihughes@gmail.com - 2015-01-26 13:30 -0800
      Re: faster than c Ian Collins <ian-news@hotmail.com> - 2015-01-18 16:36 +1300
    Re: faster than c glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-01-17 19:02 +0000
      Re: faster than c BartC <bc@freeuk.com> - 2015-01-17 19:14 +0000
        Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-17 11:41 -0800
          Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-17 12:06 -0800
            Re: faster than c BartC <bc@freeuk.com> - 2015-01-17 20:36 +0000
              Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-17 13:01 -0800
                Re: faster than c BartC <bc@freeuk.com> - 2015-01-17 21:18 +0000
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-17 13:36 -0800
                Re: faster than c David Brown <david.brown@hesbynett.no> - 2015-01-19 11:17 +0100
                Re: faster than c Richard Heathfield <invalid@see.sig.invalid> - 2015-01-19 10:40 +0000
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-19 03:29 -0800
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-19 03:41 -0800
                Re: faster than c David Brown <david.brown@hesbynett.no> - 2015-01-19 12:45 +0100
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-19 04:00 -0800
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-19 04:31 -0800
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-19 04:48 -0800
                Re: faster than c David Brown <david.brown@hesbynett.no> - 2015-01-19 15:23 +0100
                Re: faster than c mark.bluemel@gmail.com - 2015-01-19 07:25 -0800
                Re: faster than c Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-19 08:56 -0800
                Re: faster than c Richard Heathfield <invalid@see.sig.invalid> - 2015-01-19 17:01 +0000
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-19 09:08 -0800
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-19 09:19 -0800
                Re: faster than c Bartc <bc@freeuk.com> - 2015-01-19 17:46 +0000
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-19 10:19 -0800
                Re: faster than c Geoff <geoff@invalid.invalid> - 2015-01-19 10:56 -0800
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-19 11:30 -0800
                Re: faster than c Keith Thompson <kst-u@mib.org> - 2015-01-19 14:22 -0800
                Re: faster than c mark.bluemel@gmail.com - 2015-01-20 00:45 -0800
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-20 01:20 -0800
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-19 08:25 -0800
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-20 00:57 -0800
                Re: faster than c fmassei@gmail.com - 2015-01-19 03:58 -0800
                Re: faster than c Robert Wessel <robertwessel2@yahoo.com> - 2015-01-20 00:05 -0600
                Re: faster than c fmassei@gmail.com - 2015-01-20 02:11 -0800
                Re: faster than c asetofsymbols@gmail.com - 2015-01-20 02:18 -0800
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-20 02:30 -0800
                Re: faster than c asetofsymbols@gmail.com - 2015-01-20 05:16 -0800
                Re: faster than c Melzzzzz <mel@zzzzz.com> - 2015-01-20 14:18 +0100
                Re: faster than c Robert Wessel <robertwessel2@yahoo.com> - 2015-01-20 15:29 -0600
                Re: faster than c Richard Heathfield <invalid@see.sig.invalid> - 2015-01-20 21:53 +0000
                Re: faster than c Robert Wessel <robertwessel2@yahoo.com> - 2015-01-20 17:37 -0600
                Re: faster than c Richard Heathfield <invalid@see.sig.invalid> - 2015-01-21 00:05 +0000
                Re: faster than c Robert Wessel <robertwessel2@yahoo.com> - 2015-01-21 01:38 -0600
                Re: faster than c Richard Heathfield <invalid@see.sig.invalid> - 2015-01-21 07:46 +0000
                Re: faster than c Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-21 03:23 -0800
                Re: faster than c Richard Heathfield <invalid@see.sig.invalid> - 2015-01-21 13:52 +0000
                Re: faster than c Morris Dovey <mrdovey@iedu.com> - 2015-01-21 08:53 -0600
                Re: faster than c Melzzzzz <mel@zzzzz.com> - 2015-01-21 01:18 +0100
                Re: faster than c fmassei@gmail.com - 2015-01-20 16:41 -0800
                Re: faster than c Melzzzzz <mel@zzzzz.com> - 2015-01-21 01:58 +0100
                Re: faster than c fmassei@gmail.com - 2015-01-20 17:11 -0800
                Re: faster than c Melzzzzz <mel@zzzzz.com> - 2015-01-21 02:18 +0100
                Re: faster than c fmassei@gmail.com - 2015-01-20 17:38 -0800
                Re: faster than c Keith Thompson <kst-u@mib.org> - 2015-01-21 08:48 -0800
                Re: faster than c Robert Wessel <robertwessel2@yahoo.com> - 2015-01-20 15:24 -0600
                Re: faster than c fmassei@gmail.com - 2015-01-20 13:38 -0800
                Re: faster than c Greg Martin <greg.at.softsprocket@dot.com> - 2015-01-21 16:43 +0000
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-21 10:09 -0800
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-24 01:15 -0800
                Re: faster than c Robert Wessel <robertwessel2@yahoo.com> - 2015-01-21 23:59 -0600
                Re: faster than c fmassei@gmail.com - 2015-01-22 01:17 -0800
                Re: faster than c wssimms@gmail.com - 2015-01-22 05:16 -0800
                Re: faster than c fmassei@gmail.com - 2015-01-22 05:27 -0800
                Re: faster than c Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-22 05:36 -0800
                Re: faster than c fmassei@gmail.com - 2015-01-22 05:54 -0800
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-22 05:55 -0800
                Re: faster than c David Brown <david.brown@hesbynett.no> - 2015-01-22 15:18 +0100
                Re: faster than c Robert Wessel <robertwessel2@yahoo.com> - 2015-01-23 00:53 -0600
                Re: faster than c fmassei@gmail.com - 2015-01-23 00:36 -0800
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-23 01:09 -0800
                Re: faster than c fmassei@gmail.com - 2015-01-23 01:33 -0800
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-23 01:55 -0800
                Re: faster than c Ian Collins <ian-news@hotmail.com> - 2015-01-23 22:44 +1300
                Re: faster than c fmassei@gmail.com - 2015-01-23 01:56 -0800
                Re: faster than c David Brown <david.brown@hesbynett.no> - 2015-01-22 15:11 +0100
                Re: faster than c fmassei@gmail.com - 2015-01-22 06:21 -0800
                Re: faster than c David Brown <david.brown@hesbynett.no> - 2015-01-22 16:46 +0100
                Re: faster than c fmassei@gmail.com - 2015-01-22 07:58 -0800
                Re: faster than c Lowell Gilbert <lgusenet@be-well.ilk.org> - 2015-01-22 12:29 -0500
                Re: faster than c fmassei@gmail.com - 2015-01-22 10:09 -0800
                Re: faster than c "Chris M. Thomasson" <no@spam.invalid> - 2015-01-22 14:07 -0800
                Re: faster than c asetofsymbols@gmail.com - 2015-01-24 00:40 -0800
                Re: faster than c "Chris M. Thomasson" <no@spam.invalid> - 2015-01-24 12:53 -0800
                Re: faster than c Greg Martin <greg.at.softsprocket@dot.com> - 2015-01-22 15:09 +0000
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-22 01:59 -0800
                Re: faster than c Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-24 09:46 -0700
                Re: faster than c Robert Wessel <robertwessel2@yahoo.com> - 2015-03-25 02:15 -0500
                Re: faster than c asetofsymbols@gmail.com - 2015-03-25 02:45 -0700
                Re: faster than c Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-25 11:30 -0700
                Re: faster than c Robert Wessel <robertwessel2@yahoo.com> - 2015-03-26 00:42 -0500
                Re: faster than c Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-28 11:35 -0700
                Re: faster than c Keith Thompson <kst-u@mib.org> - 2015-01-17 14:42 -0800
                Re: faster than c BartC <bc@freeuk.com> - 2015-01-17 22:53 +0000
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-17 15:13 -0800
        Re: faster than c glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-01-17 22:02 +0000
        Re: faster than c Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-17 16:21 -0800
          Re: faster than c BartC <bc@freeuk.com> - 2015-01-18 01:27 +0000
            Re: faster than c glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-01-18 01:41 +0000
            Re: faster than c Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-18 00:11 -0800
              Re: faster than c BartC <bc@freeuk.com> - 2015-01-18 11:23 +0000
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-18 05:55 -0800
                Re: faster than c BartC <bc@freeuk.com> - 2015-01-18 20:11 +0000
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-18 13:12 -0800
                Re: faster than c BartC <bc@freeuk.com> - 2015-01-18 23:11 +0000
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-18 15:49 -0800
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-18 16:06 -0800
                Re: faster than c Bartc <bc@freeuk.com> - 2015-01-19 01:03 +0000
                Re: faster than c Richard Heathfield <invalid@see.sig.invalid> - 2015-01-19 08:13 +0000
                Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-19 01:39 -0800
                Re: faster than c Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-01-18 13:52 -0800
                Re: faster than c BartC <bc@freeuk.com> - 2015-01-18 23:36 +0000
      Re: faster than c Walter Banks <walter@bytecraft.com> - 2015-01-20 14:01 -0500
        Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-20 11:18 -0800
    Re: faster than c Melzzzzz <mel@zzzzz.com> - 2015-01-17 22:30 +0100
    Re: faster than c Ian Collins <ian-news@hotmail.com> - 2015-01-18 16:49 +1300
  Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-17 05:55 -0800
    Re: faster than c BartC <bc@freeuk.com> - 2015-01-17 14:30 +0000
      Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-17 07:32 -0800
        Re: faster than c BartC <bc@freeuk.com> - 2015-01-17 15:56 +0000
          Re: faster than c fir <profesor.fir@gmail.com> - 2015-01-17 08:33 -0800
  Re: faster than c fmassei@gmail.com - 2015-01-19 02:26 -0800
  faster than c asetofsymbols@gmail.com - 2015-01-20 00:12 -0800

csiph-web