Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #151130
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: The big problem of C |
| Date | 2020-04-06 13:36 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <r6f47c$992$1@dont-email.me> (permalink) |
| References | (2 earlier) <92fc2d76-4eaa-4fab-b188-d63eec8c3429@googlegroups.com> <aijnlg-evl1.ln1@mail.home.palmen-it.de> <r6c282$mev$1@news.albasani.net> <r6deg3$ki7$1@dont-email.me> <r6eohg$s54$2@news.albasani.net> |
On 06/04/2020 10:16, Bonita Montero wrote:
>
>> In Python, that can be done in two lines:
>> import fileinput
>> print('\n'.join(sorted(fileinput.input())))
>> C++ takes an order of magnitude more code. C++ is simply a mess.
>
> If you need more performance than Pythong or bash,
> C++ would be the proper language.
I ran a quick test with the Complete Works of Shakespeare as a text file
(thanks, Project Gutenberg!) comparing your C++ program to other solutions:
Linux "sort":
0.208 s real time, 0.329 s user time (i.e., it is threaded)
C++:
0.203 s real time, 0.146 s user time
Python 3:
0.166 s real time, 0.153 s user time
Python 2:
0.106 s real time, 0.101 s user time
Pypy:
0.098 s real time, 0.086 s user time
With a file made from 10 copies, the numbers are now:
Linux "sort":
1.6 s real time, 6.6 s user time (i.e., it is threaded)
C++:
2.2 s real time, 1.5 s user time
Python 3:
1.8 s real time, 1.7 s user time
Python 2:
1.3 s real time, 1.3 s user time
Pypy:
1.2 s real time, 1.1 s user time
With a bigger file, C++ is now the /slowest/ option. Slower than
off-the-shelf "sort", slower than Python. (And yes, the C++ code was
compiled with optimisation.)
Obviously this is a simple sample. And obviously different methods have
their advantages and disadvantages, and all could be optimised or
specialised in different ways to be better at some sorting while worse
at others.
For example, a simple C program that can handle up to 10,000,000 lines
of up to 128 characters can be written:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
enum { maxLine = 128 };
enum { maxLineCount = 10000000 };
static char lines[maxLineCount][maxLine];
static int indexes[maxLineCount];
int comp(const void * a, const void * b) {
const int * ap = (const int *) a;
const int * bp = (const int *) b;
return strcmp(lines[*ap], lines[*bp]);
}
int main(void) {
int n = 0;
while (fgets(lines[n], maxLine - 1, stdin)) n++;
for (int i = 0; i < n; i++) {
indexes[i] = i;
}
qsort(indexes, n, sizeof(int), comp);
for (int i = 0; i < n; i++) {
fputs(lines[indexes[i]], stdout);
}
}
For the original test file, this gave:
0.047 s real time, 0.043 s user time
For the ten times bigger file, it gave :
0.586 s real time, 0.513 s user time
It is about 4 times as fast as the C++ code, and twice as fast as the
Python code. But it only works within certain limitations.
What can we see from this? We can see that anyone who says "language X
is the fastest" is simplifying so much they are just wrong. Anyone who
says "language X is a mess" is doing likewise.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
The big problem of C Thiago Adams <thiago.adams@gmail.com> - 2020-04-04 13:41 -0700
Re: The big problem of C felix@palmen-it.de (Felix Palmen) - 2020-04-04 23:18 +0200
Re: The big problem of C Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-04-04 14:24 -0700
Re: The big problem of C felix@palmen-it.de (Felix Palmen) - 2020-04-04 23:31 +0200
Re: The big problem of C Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-04-04 14:39 -0700
Re: The big problem of C Bart <bc@freeuk.com> - 2020-04-04 23:08 +0100
Re: The big problem of C Thiago Adams <thiago.adams@gmail.com> - 2020-04-04 16:48 -0700
Re: The big problem of C BGB <cr88192@gmail.com> - 2020-04-04 20:06 -0500
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-05 19:44 +0200
Re: The big problem of C Thiago Adams <thiago.adams@gmail.com> - 2020-04-04 16:40 -0700
Re: The big problem of C felix@palmen-it.de (Felix Palmen) - 2020-04-05 01:58 +0200
Re: The big problem of C Thiago Adams <thiago.adams@gmail.com> - 2020-04-04 18:05 -0700
Re: The big problem of C Ian Collins <ian-news@hotmail.com> - 2020-04-05 15:17 +1200
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-05 09:44 +0200
Re: The big problem of C felix@palmen-it.de (Felix Palmen) - 2020-04-05 11:14 +0200
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-05 11:27 +0200
Re: The big problem of C felix@palmen-it.de (Felix Palmen) - 2020-04-05 11:34 +0200
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-05 11:45 +0200
Re: The big problem of C nobody@example.org (Scott) - 2020-04-05 16:27 +0000
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-05 19:38 +0200
Re: The big problem of C David Brown <david.brown@hesbynett.no> - 2020-04-05 22:19 +0200
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-06 10:16 +0200
Re: The big problem of C David Brown <david.brown@hesbynett.no> - 2020-04-06 13:36 +0200
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-06 13:55 +0200
Re: The big problem of C Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-04-06 05:04 -0700
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-06 14:11 +0200
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-06 18:01 +0200
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-06 22:08 +0200
Re: The big problem of C Öö Tiib <ootiib@hot.ee> - 2020-04-06 20:47 -0700
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-07 09:06 +0200
Re: The big problem of C already5chosen@yahoo.com - 2020-04-07 05:58 -0700
Re: The big problem of C David Brown <david.brown@hesbynett.no> - 2020-04-07 17:55 +0200
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-07 19:02 +0200
Re: The big problem of C already5chosen@yahoo.com - 2020-04-07 10:21 -0700
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-07 20:02 +0200
Re: The big problem of C already5chosen@yahoo.com - 2020-04-07 13:10 -0700
Re: The big problem of C felix@palmen-it.de (Felix Palmen) - 2020-04-07 22:34 +0200
Railway programming (Was : The big problem of C) Spiros Bousbouras <spibou@gmail.com> - 2020-04-08 00:01 +0000
Re: Railway programming (Was : The big problem of C) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-04-08 01:11 +0100
Re: Railway programming (Was : The big problem of C) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-04-08 01:12 +0100
Re: Railway programming (Was : The big problem of C) felix@palmen-it.de (Felix Palmen) - 2020-04-08 09:56 +0200
Re: Railway programming (Was : The big problem of C) Spiros Bousbouras <spibou@gmail.com> - 2020-04-09 11:55 +0000
Re: Railway programming (Was : The big problem of C) Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-04-08 08:17 +0000
Re: Railway programming (Was : The big problem of C) felix@palmen-it.de (Felix Palmen) - 2020-04-08 09:42 +0200
Re: The big problem of C already5chosen@yahoo.com - 2020-04-07 06:13 -0700
Re: The big problem of C Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-04-07 09:17 -0700
Re: The big problem of C already5chosen@yahoo.com - 2020-04-07 09:44 -0700
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-07 09:07 +0200
Re: The big problem of C Öö Tiib <ootiib@hot.ee> - 2020-04-06 05:01 -0700
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-06 14:42 +0200
Re: The big problem of C Bart <bc@freeuk.com> - 2020-04-06 00:09 +0100
Re: The big problem of C David Brown <david.brown@hesbynett.no> - 2020-04-06 09:09 +0200
Re: The big problem of C Bart <bc@freeuk.com> - 2020-04-05 12:41 +0100
Re: The big problem of C Thiago Adams <thiago.adams@gmail.com> - 2020-04-05 07:05 -0700
Re: The big problem of C BGB <cr88192@gmail.com> - 2020-04-04 17:48 -0500
Re: The big problem of C Thiago Adams <thiago.adams@gmail.com> - 2020-04-04 17:16 -0700
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-05 09:25 +0200
Re: The big problem of C jacobnavia <jacob@jacob.remcomp.fr> - 2020-04-05 09:50 +0200
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-05 09:53 +0200
Re: The big problem of C jacobnavia <jacob@jacob.remcomp.fr> - 2020-04-05 18:00 +0200
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-05 19:35 +0200
Re: The big problem of C jacobnavia <jacob@jacob.remcomp.fr> - 2020-04-06 08:05 +0200
Re: The big problem of C Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-04-05 04:25 -0700
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-05 13:32 +0200
Re: The big problem of C Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-04-05 04:57 -0700
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-05 14:09 +0200
Re: The big problem of C chad <cdalten@gmail.com> - 2020-04-05 05:20 -0700
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-05 15:04 +0200
Re: The big problem of C Öö Tiib <ootiib@hot.ee> - 2020-04-05 05:22 -0700
Re: The big problem of C Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-04-05 05:33 -0700
Re: The big problem of C Öö Tiib <ootiib@hot.ee> - 2020-04-05 06:55 -0700
Re: The big problem of C felix@palmen-it.de (Felix Palmen) - 2020-04-05 16:42 +0200
Re: The big problem of C BGB <cr88192@gmail.com> - 2020-04-05 09:57 -0500
Re: The big problem of C Spiros Bousbouras <spibou@gmail.com> - 2020-04-06 10:46 +0000
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-06 13:14 +0200
Re: The big problem of C Philipp Klaus Krause <pkk@spth.de> - 2020-04-05 19:00 +0200
Re: The big problem of C Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-04-05 10:08 -0700
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-05 19:52 +0200
Re: The big problem of C BGB <cr88192@gmail.com> - 2020-04-05 13:37 -0500
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-05 20:49 +0200
Re: The big problem of C BGB <cr88192@gmail.com> - 2020-04-05 15:27 -0500
Re: The big problem of C Bonita Montero <Bonita.Montero@gmail.com> - 2020-04-06 10:14 +0200
Re: The big problem of C Thiago Adams <thiago.adams@gmail.com> - 2020-04-05 12:07 -0700
Re: The big problem of C Guillaume <message@bottle.org> - 2020-04-06 15:09 +0200
Re: The big problem of C Thiago Adams <thiago.adams@gmail.com> - 2020-04-06 07:02 -0700
Re: The big problem of C BGB <cr88192@gmail.com> - 2020-04-06 10:30 -0500
Re: The big problem of C Thiago Adams <thiago.adams@gmail.com> - 2020-04-06 09:06 -0700
Re: The big problem of C Guillaume <message@bottle.org> - 2020-04-06 18:43 +0200
Re: The big problem of C Thiago Adams <thiago.adams@gmail.com> - 2020-04-06 12:19 -0700
Re: The big problem of C Thiago Adams <thiago.adams@gmail.com> - 2020-04-08 18:27 -0700
csiph-web