....
snip Perl code
....
D:\Home\asu1\UseNet\clpmisc\r> test
Benchmark: running ls for at least 1 CPU seconds...
ls: 1 wallclock secs ( 0.76 usr + 0.31 sys = 1.08 CPU) @ 99.35/s
(n=107)
This is on Windows XPSP2, AMD64 running at 1.8Ghz, 1Gb RAM, about 402
MB allocated.
So we get about 100 readdirs in list context per second.
What is the equivalent C program you tested?
The following C program is really not the equivalent of the Perl program
I posted, but it does copy the names, and creates a list of file names
etc.
I first ran a do-nothing version which called an empty ls() function 100
times to get a baseline timing. The time reported by the Windows'
timethis utility reported an average of 0.16 seconds.
Then I wrote the following:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
void ls(size_t num_files) {
struct dirent *ent;
DIR *dir;
size_t f;
char **list = malloc(1 + num_files * sizeof(*list) );
if( !list ) {
fprintf(stderr, "Memory allocation error\n");
exit(EXIT_FAILURE);
}
dir = opendir(".");
if( !dir ) {
perror("Cannot open '.'");
}
for(f = 0; f != num_files; ++f) {
char *d_name;
ent = readdir(dir);
if( !ent ) {
break;
}
d_name = malloc(1 + strlen(ent->d_name));
if( !d_name ) {
break;
}
strcpy(d_name, ent->d_name);
list[f] = d_name;
}
list[f] = NULL;
}
int main(void) {
struct dirent *ent;
size_t num_files = 0;
DIR *dir = opendir(".");
if( !dir ) {
perror("Cannot open '.'");
}
while(ent = readdir(dir)) {
++num_files;
}
if( closedir(dir) ) {
perror("Cannot close '.'");
}
{
int i;
for(i = 0; i != 100; ++i) {
ls(num_files);
}
}
return 0;
}
D:\Home\asu1\UseNet\clpmisc\r> gcc -Wall -O2 r.c -o r.exe
D:\Home\asu1\UseNet\clpmisc\r> timethis r.exe
TimeThis : Command Line : r.exe
TimeThis : Start Time : Wed Sep 21 20:20:45 2005
TimeThis : End Time : Wed Sep 21 20:20:47 2005
TimeThis : Elapsed Time : 00:00:01.640
So, again, we get about 100 readdirs per second in list context (so to
speak). Now, clearly, I am not a great C programmer, but I would be
interested to see the C program that generates the vastly superior
timings.
Sinan