K
Khookie
Hi everyone
BTW, thanks to everyone in this group, your collective advice has been
very helpful. I have to say, the C guys are definitely much nicer
than the Lisp guys ;-).
Tonight, I was thinking about freads, and how to get them faster. I
initially wrote a program like this:
-- START CODE --
#include <stdio.h>
#include <time.h>
#define BUFSIZE 32768
#define CHAR_SIZE 1
int main(int argc, char **argv) {
clock_t start = clock();
char buf[BUFSIZE + 1];
memset(buf, '\0', BUFSIZE + 1);
FILE *file = fopen("stuff.txt", "rb");
while (fread(buf, BUFSIZE, CHAR_SIZE, file)) {
//printf("%s", buf);
memset(buf, '\0', BUFSIZE);
}
//printf("%s", buf);
fclose(file);
clock_t finish = clock();
printf("Total CPU time: %d\n", finish - start);
}
-- END CODE --
Average CPU time: 140-156
BTW, stuff.txt is a 200MB binary file of randomness.
I looked at the memsets, and thought - this could maybe be faster.
-- START CODE --
#include <stdio.h>
#include <time.h>
#define BUFSIZE 32768
#define CHAR_SIZE 1
int main(int argc, char **argv) {
clock_t start = clock();
char buf[BUFSIZE + 1];
buf[BUFSIZE] = '\0';
FILE *file = fopen("stuff.txt", "rb");
// Prepare
fseek(file, 0 , SEEK_END);
int size = ftell(file);
int iterations = size / BUFSIZE;
int remaining = size % BUFSIZE;
rewind(file);
/*
printf("Size: %d\n", size);
printf("Iterations: %d\n", iterations);
printf("Remainder: %d\n", remaining);
*/
// Iterate
int i;
for (i = 0; i < iterations; i++) {
fread(buf, BUFSIZE, CHAR_SIZE, file);
//printf("%s", buf);
}
fread(buf, BUFSIZE, CHAR_SIZE, file);
buf[remaining] = '\0';
//printf("%s", buf);
fclose(file);
clock_t finish = clock();
printf("Total CPU time: %d\n", finish - start);
}
-- END CODE --
Average CPU time: 125
What does everyone think? Could it be better?
Hope this was useful to someone.
Chris
BTW, thanks to everyone in this group, your collective advice has been
very helpful. I have to say, the C guys are definitely much nicer
than the Lisp guys ;-).
Tonight, I was thinking about freads, and how to get them faster. I
initially wrote a program like this:
-- START CODE --
#include <stdio.h>
#include <time.h>
#define BUFSIZE 32768
#define CHAR_SIZE 1
int main(int argc, char **argv) {
clock_t start = clock();
char buf[BUFSIZE + 1];
memset(buf, '\0', BUFSIZE + 1);
FILE *file = fopen("stuff.txt", "rb");
while (fread(buf, BUFSIZE, CHAR_SIZE, file)) {
//printf("%s", buf);
memset(buf, '\0', BUFSIZE);
}
//printf("%s", buf);
fclose(file);
clock_t finish = clock();
printf("Total CPU time: %d\n", finish - start);
}
-- END CODE --
Average CPU time: 140-156
BTW, stuff.txt is a 200MB binary file of randomness.
I looked at the memsets, and thought - this could maybe be faster.
-- START CODE --
#include <stdio.h>
#include <time.h>
#define BUFSIZE 32768
#define CHAR_SIZE 1
int main(int argc, char **argv) {
clock_t start = clock();
char buf[BUFSIZE + 1];
buf[BUFSIZE] = '\0';
FILE *file = fopen("stuff.txt", "rb");
// Prepare
fseek(file, 0 , SEEK_END);
int size = ftell(file);
int iterations = size / BUFSIZE;
int remaining = size % BUFSIZE;
rewind(file);
/*
printf("Size: %d\n", size);
printf("Iterations: %d\n", iterations);
printf("Remainder: %d\n", remaining);
*/
// Iterate
int i;
for (i = 0; i < iterations; i++) {
fread(buf, BUFSIZE, CHAR_SIZE, file);
//printf("%s", buf);
}
fread(buf, BUFSIZE, CHAR_SIZE, file);
buf[remaining] = '\0';
//printf("%s", buf);
fclose(file);
clock_t finish = clock();
printf("Total CPU time: %d\n", finish - start);
}
-- END CODE --
Average CPU time: 125
What does everyone think? Could it be better?
Hope this was useful to someone.
Chris