Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* C++ best of 10 reads (7200 rpm laptop drive):
- * read/stored 25481 lines in 0.00813842 sec.
- * total heap usage: 76,465 allocs, 76,464 frees, 2,151,883 bytes allocated
- * (reading, e.g. /usr/share/dict/words)
- */
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- #include <string>
- #include <list>
- #include <time.h>
- using namespace std;
- /** return the number of seconds (nanosecond resolution)
- * using clock_gettime/CLOCK_REALTIME
- */
- double clkrtv (void)
- {
- struct timespec ts;
- double sec;
- clock_gettime (CLOCK_REALTIME, &ts);
- sec = ts.tv_nsec;
- sec /= 1e9;
- sec += ts.tv_sec;
- return sec;
- }
- /* output lines read/stored and time taken */
- void printlist (list<string> *l, double tm) {
- cout << "read/stored " << l->size() << " lines in "
- << tm << " sec." << endl;
- }
- int main (int argc, char **argv) {
- double t1, t2;
- string line;
- list<string> buf; // doublely-linked list of string
- if (argc != 2) {
- cerr << "One argument is required." << endl;
- return 1;
- }
- ifstream f (argv[1]); // open file
- t1 = clkrtv();
- while (getline (f, line)) { // read lines into buf
- buf.push_back(line);
- }
- t2 = clkrtv();
- f.close();
- printlist (&buf, t2-t1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement