User time: 109.45000 (sec) system time: 1.05000 (sec) Average duration: 11.05000 (sec)
User time: 69.85(sec) system time: 0.70(sec) Average duration: 7.055(sec)
Input.txt was generated with create_input.cxx
/* cc -O5 -Wall -o c_strings c_strings.c */
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/times.h>
#define TOTAL_LOOPS 10
int main(int argc, char *argv[])
{
char* f_name="input.txt";
char* search_word="hello";
FILE* words;
int i;
struct tms start;
struct tms finish;
/* syntax: c_strings <file_name> <search_word> */
if(argc > 1) {
f_name=argv[1];
if(argc > 2) {
search_word=argv[2];
}
}
words=fopen(f_name, "r");
if(!words) {
fprintf(stderr, "Unable to open file: %s\n", f_name);
return -1;
}
times(&start);
for(i=0; i < TOTAL_LOOPS; i++) {
long start = clock();
int i=0;
do {
char test_word[1024];
fscanf(words, "%s", test_word); ++i;
if(!strcmp(test_word, search_word)) {
printf("Found(%d): %s\n", i, search_word);
}
} while(!feof(words));
printf("Duration: %2.5f\n", (double)(clock()-start)/CLOCKS_PER_SEC);
fseek(words, 0L, SEEK_SET);
}
times(&finish);
printf("*****\n"
"User time: %2.5f (sec) system time: %2.5f (sec)\n"
"Average duration: %2.5f (sec)\n"
"*****\n",
(double)(finish.tms_utime-start.tms_utime)/100,
(double)(finish.tms_stime-start.tms_stime)/100,
(double)((finish.tms_utime-start.tms_utime)+
(finish.tms_stime-start.tms_stime))/100/TOTAL_LOOPS);
fclose(words);
return 0;
}
// c++ -O5 -Wall -o strings strings.cxx
#include <string>
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <sys/times.h>
#include <time.h>
using namespace std ;
int main(int argc, char *argv[])
{
const char* f_name="input.txt";
string search_word="hello";
// calling syntax: ./strings <file_name> <search_word>
if(argc > 1) {
f_name=argv[1];
if(argc > 2) {
search_word=argv[2];
}
}
ifstream words(f_name, ios::in|ios::nocreate);
if(!words.good()) {
cerr << "Unable to open file: " << f_name << endl;
return -1;
}
struct tms start;
times(&start);
const int total_loops=10;
cout << setprecision(5);
for(int i = 0; i < total_loops; i++) {
long start=clock();
int i=0;
do {
char test_word[1024];
words >> test_word;
++i;
if(search_word == test_word) {
cout << "Found(" << i << "): " << test_word << endl;
}
} while(!words.eof());
cout << "Duration: "<<(double)(clock()-start)/CLOCKS_PER_SEC<<endl;
words.seekg(0, ios::beg);
words.clear(); // we just clear eof flag.
}
struct tms finish;
times(&finish);
cout << "*****\n"
"User time: " <<
(double)(finish.tms_utime-start.tms_utime)/100
<< "(sec) system time: " <<
(double)(finish.tms_stime-start.tms_stime)/100
<< "(sec)\n"
"Average duration: " <<
(double)((finish.tms_utime-start.tms_utime)+
(finish.tms_stime-start.tms_stime))/100/total_loops <<
"(sec)\n"
"*****\n";
return 0;
}
// c++ -O5 -Wall -o create_input create_input.cxx
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
const int output_size=5*1024*1024; // 5 MB
const int block_size=80; // a line
const int common_word_length=5;
#define HELLO_MESSAGES 1
const char f_name[]="input.txt";
int main(int, char*[])
{
ofstream out_f(f_name);
if(!out_f) {
cerr << "Unable to write file: " << f_name << endl;
return -1;
}
#if HELLO_MESSAGES==1
bool already=false;
#endif
for(int n=output_size/block_size; n--;) {
char block_buffer[block_size];
for(unsigned i=0; i < sizeof(block_buffer); i++) {
if(!(rand()%common_word_length)) {
block_buffer[i]=' ';
} else {
block_buffer[i]=(char)(rand()%('z'-'a'))+'a';
}
}
out_f.write(block_buffer, sizeof(block_buffer));
out_f.put('\n'); // out_f<<endl; causes flush, we do not need it.
#if HELLO_MESSAGES != 1
if(!( rand() % (output_size/block_size/HELLO_MESSAGES) )) {
out_f << "hello\n"; // We are going to find this word.
}
#else
if(!already && !( rand() % ((output_size/block_size)>>1) )) {
out_f << "hello\n";
already=true;
}
#endif
}
return 0;
}