As promised, after football.
At first I thought of making a small training sketch for @ navi1893 , and I began to simply write options for counting the number of lines, but I became interested in the time of their execution.
I can not share the results.
C ++ program. I did it in MinGW g ++ on Win 7, and then in Linux g ++ under VBox on the same machine (I5-2500 3.30 Ghz).
So, the program itself (lines.cpp file) In it, in the comments, information about the execution time in Windows.
#include <iostream> #include <cstdio> #include <cstdlib> #include <unistd.h> using namespace std; extern "C" long long mtime(); int main (int ac, char *av[]) { long long start = mtime(); int k = 0, ci, co = 0, how = av[1]? atoi(av[1]): 0; char c; string s; /* 10000001 lines (88888904 bytes) Windows 7 I5-2500 3.30 GHz */ switch (how) { case 0: cout << "getchar() countlines\n"; /* 700 msec */ while ((ci = getchar()) != EOF) { co = ci; if (ci == '\n') k++; } if (co && co != '\n') k++; break; case 1: cout << "cin.get(char) countlines\n"; /* 10300 msec */ while (cin.get(c) && !cin.eof()) { co = c; if (c == '\n') k++; } if (co && co != '\n') k++; break; case 2: cout << "read(0,char *,4096) countlines\n"; /* 450 msec */ int len, llen; char buf[4096]; llen = 0; while (len = read(0,buf,sizeof(buf))) { int i; for (i = 0; i < len; i++) { if (buf[i] == '\n') k++; } llen = len; } if (llen && buf[llen-1] != '\n') k++; break; default: cout << "getline(cin,string) countlines\n"; /* 14150 msec */ while (!cin.eof()) { getline(cin,s); if (cin.good() || s.length()) k++; } } cout <<k<< " lines\n" << mtime()-start <<" msec\n"; exit(0); }
As you can see, 4 ways (almost the same) of counting the number of rows are implemented.
The program for creating a test file (file mkli.c)
#include <stdio.h> #include <stdlib.h> int main (int ac, char *av[]) { int n = av[1]? atoi(av[1]): 10000000; if (n <= 0) n = 1000; printf ("n = %d\n",n); int i; for (i = 0; i < n; i++) printf ("%d\n",i); exit (0); }
Text function for measuring time in milliseconds (mtime.c file)
/* время в миллисекундах */ #include <stdio.h> #include <sys/time.h> long long mtime() { struct timeval t; gettimeofday(&t, NULL); long long mt = (long long)t.tv_sec * 1000 + t.tv_usec / 1000; return mt; }
But the protocol for broadcasting and running programs in Linux (and the execution times in it)
avp@avp-ubu1:~/hashcode$ avp@avp-ubu1:~/hashcode$ gcc -c mtime.c avp@avp-ubu1:~/hashcode$ gcc mkli.c -o mkli avp@avp-ubu1:~/hashcode$ g++ lines.cpp mtime.o -o lines avp@avp-ubu1:~/hashcode$ ./mkli >li.tst avp@avp-ubu1:~/hashcode$ wc li.tst 10000001 10000003 78888903 li.tst avp@avp-ubu1:~/hashcode$ avp@avp-ubu1:~/hashcode$ ./lines 0 < li.tst getchar() countlines 10000001 lines 685 msec avp@avp-ubu1:~/hashcode$ ./lines 1 < li.tst cin.get(char) countlines 10000001 lines 3101 msec avp@avp-ubu1:~/hashcode$ ./lines 2 < li.tst read(0,char *,4096) countlines 10000001 lines 225 msec avp@avp-ubu1:~/hashcode$ ./lines 3 < li.tst getline(cin,string) countlines 10000001 lines 2732 msec avp@avp-ubu1:~/hashcode$
I hope it will be informative.
UPDATE 1
Especially for @ navi1893 .
File navinl.cpp
$ g++ navinl.cpp -o navinl $ ./navinl.exe < navinl.cpp 21 lines $ #include <iostream> #include <cstdio> using namespace std; int main (int ac, char *av[]) { int ci, co = 0, k = 0; while ((ci = getchar()) != EOF) { co = ci; if (ci == '\n') k++; } if (co && co != '\n') k++; cout <<k<< " lines\n"; return 0; }
As for the variables ci and co .
Lines in the file are separated by '\ n'. It would seem that to determine the number of lines, it is enough to count the number of NEWLINE characters in the file.
However, if the last portion of the data in the file does not end with '\ n', then we will not consider it as a string (with the "obvious" algorithm). Therefore, after finding the end of the file, it is necessary to check whether the last character in the file '\ n' or not. For this purpose, we use the variable co , in which we save the last character read.
Why the libc getchar () IMHO is used for reading the file (standard input) is obvious from the previous material.