Re: cat
On Mar 6, 12:35 pm, user923005 <dcor...@connx. comwrote:
The real problem here is that catfilebufferfg ets() needs to be made
much more robust. But it does lend a lot of speed (probably due to
the much lower number of function calls and tests).
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static char string[1024 * 16];
void catfilebufferfg ets(FILE * in, FILE * out)
{
setvbuf(in, NULL, _IOFBF, 1024 * 16);
setvbuf(out, NULL, _IOFBF, 1024 * 16);
/* Get characters (ERROR PRONE: {what if string 16K}) */
while (fgets(string, sizeof string, in)) {
fputs(string, out);
}
}
void catfilebuffer(F ILE * in, FILE * out)
{
register int num_char;
setvbuf(in, NULL, _IOFBF, 1024 * 16);
setvbuf(out, NULL, _IOFBF, 1024 * 16);
/* Get characters */
while ((num_char = getc(in)) != EOF) {
/* Print to standard output */
putc(num_char, out);
}
}
void catfilenobuff(F ILE * in, FILE * out)
{
register int num_char;
/* Get characters */
while ((num_char = getc(in)) != EOF) {
/* Print to standard output */
putc(num_char, out);
}
}
int main(int argc, char **argv)
{
FILE *in = stdin;
FILE *out = stdout;
clock_t start,
end;
static const double cps = 1.0 / CLOCKS_PER_SEC;
if (argc 1) {
in = fopen(argv[1], "r");
if (in == NULL) {
printf("Error opening %s\n", argv[1]);
exit(EXIT_FAILU RE);
}
}
if (argc 2) {
out = fopen(argv[2], "w");
if (out == NULL) {
printf("Error opening %s\n", argv[2]);
exit(EXIT_FAILU RE);
}
}
start = clock();
catfilenobuff(i n, out);
end = clock();
printf("standar d cat took %f seconds\n", (end - start) * cps);
rewind(in);
fclose(out);
if (argc 2) {
out = fopen(argv[2], "w");
if (out == NULL) {
printf("Error opening %s\n", argv[2]);
exit(EXIT_FAILU RE);
}
} else
out = stdout;
start = clock();
catfilebuffer(i n, out);
end = clock();
printf("big buffer cat took %f seconds\n", (end - start) * cps);
rewind(in);
fclose(out);
if (argc 2) {
out = fopen(argv[2], "w");
if (out == NULL) {
printf("Error opening %s\n", argv[2]);
exit(EXIT_FAILU RE);
}
} else
out = stdout;
start = clock();
catfilebufferfg ets(in, out);
end = clock();
printf("big buffer cat using fgets took %f seconds\n", (end -
start) * cps);
fflush(NULL);
return 0;
}
/*
C:\tmp>cat dict.sql dict.out
standard cat took 2.062000 seconds
big buffer cat took 2.016000 seconds
big buffer cat using fgets took 0.203000 seconds
*/
On Mar 6, 12:35 pm, user923005 <dcor...@connx. comwrote:
On Mar 6, 12:30 pm, user923005 <dcor...@connx. comwrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void catfilebuffer(F ILE * in, FILE * out)
{
register int num_char;
>
setvbuf(in, NULL, _IOFBF, 1024 * 16);
setvbuf(out, NULL, _IOFBF, 1024 * 16);
>
/* Get characters */
while ((num_char = getc(in)) != EOF) {
/* Print to standard output */
putc(num_char, out);
}
>
}
>
void catfilenobuff(F ILE * in, FILE * out)
{
register int num_char;
>
/* Get characters */
while ((num_char = getc(in)) != EOF) {
/* Print to standard output */
putc(num_char, out);
}
>
}
>
int main(int argc, char **argv)
{
FILE *in = stdin;
FILE *out = stdout;
clock_t start,
end;
static const double cps = 1.0 / CLOCKS_PER_SEC;
if (argc 1) {
in = fopen(argv[1], "r");
if (in == NULL) {
printf("Error opening %s\n", argv[1]);
exit(EXIT_FAILU RE);
}
}
if (argc 2) {
out = fopen(argv[2], "w");
if (out == NULL) {
printf("Error opening %s\n", argv[2]);
exit(EXIT_FAILU RE);
}
}
start = clock();
catfilenobuff(i n, out);
end = clock();
printf("standar d cat took %f seconds\n", (end - start) * cps);
rewind(in);
fclose(out);
if (argc 2) {
out = fopen(argv[2], "w");
if (out == NULL) {
printf("Error opening %s\n", argv[2]);
exit(EXIT_FAILU RE);
}
} else
out = stdout;
start = clock();
catfilebuffer(i n, out);
end = clock();
printf("big buffer cat took %f seconds\n", (end - start) * cps);
fflush(NULL);
return 0;}
>
/*
Not nearly so dramatic! ;-)
C:\tmp>cat dict.sql dict.out
standard cat took 1.968000 seconds
big buffer cat took 1.891000 seconds
*/- Hide quoted text -
>
- Show quoted text -
>
>
>
>
>
On Mar 5, 6:06 pm, Jag <talon....@gmai l.comwrote:
I've read parts of K&R's ANSI C v2 and this is what their cat looked
like but when I compared the speed of this code to gnu cat, it seems
very slow. How do I optimize this for greater speeds? is there an
alternative algorithm?
like but when I compared the speed of this code to gnu cat, it seems
very slow. How do I optimize this for greater speeds? is there an
alternative algorithm?
void catfile(FILE *in, FILE *out) {
register int num_char;
register int num_char;
/*Get characters*/
while ((num_char = getc(in)) != EOF) {
/*Print to standard output*/
putc(num_char, out);
}
while ((num_char = getc(in)) != EOF) {
/*Print to standard output*/
putc(num_char, out);
}
}
C:\tmp>dir dict.sql
Volume in drive C has no label.
Volume Serial Number is 0890-87CA
Volume in drive C has no label.
Volume Serial Number is 0890-87CA
Directory of C:\tmp
03/01/2007 11:48 AM 7,127,408 dict.sql
1 File(s) 7,127,408 bytes
0 Dir(s) 5,202,309,120 bytes free
1 File(s) 7,127,408 bytes
0 Dir(s) 5,202,309,120 bytes free
C:\tmp>cat dict.sql dict.out
standard cat took 1.984000 seconds
big buffer cat took 0.000000 seconds
standard cat took 1.984000 seconds
big buffer cat took 0.000000 seconds
C:\tmp>type cat.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void catfilebuffer(F ILE * in, FILE * out)
{
register int num_char;
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void catfilebuffer(F ILE * in, FILE * out)
{
register int num_char;
setvbuf(in, NULL, _IOFBF, 1024 * 16);
setvbuf(out, NULL, _IOFBF, 1024 * 16);
setvbuf(out, NULL, _IOFBF, 1024 * 16);
/* Get characters */
while ((num_char = getc(in)) != EOF) {
/* Print to standard output */
putc(num_char, out);
}
while ((num_char = getc(in)) != EOF) {
/* Print to standard output */
putc(num_char, out);
}
}
void catfilenobuff(F ILE * in, FILE * out)
{
register int num_char;
{
register int num_char;
/* Get characters */
while ((num_char = getc(in)) != EOF) {
/* Print to standard output */
putc(num_char, out);
}
while ((num_char = getc(in)) != EOF) {
/* Print to standard output */
putc(num_char, out);
}
}
int main(int argc, char **argv)
{
FILE *in = stdin;
FILE *out = stdout;
clock_t start,
end;
static const double cps = 1.0 / CLOCKS_PER_SEC;
if (argc 1) {
in = fopen(argv[1], "r");
if (in == NULL) {
printf("Error opening %s\n", argv[1]);
exit(EXIT_FAILU RE);
}
}
if (argc 2) {
out = fopen(argv[2], "w");
if (out == NULL) {
printf("Error opening %s\n", argv[2]);
exit(EXIT_FAILU RE);
}
}
start = clock();
catfilenobuff(i n, out);
end = clock();
printf("standar d cat took %f seconds\n", (end - start) * cps);
start = clock();
catfilebuffer(i n, out);
end = clock();
printf("big buffer cat took %f seconds\n", (end - start) * cps);
fflush(NULL);
return 0;
{
FILE *in = stdin;
FILE *out = stdout;
clock_t start,
end;
static const double cps = 1.0 / CLOCKS_PER_SEC;
if (argc 1) {
in = fopen(argv[1], "r");
if (in == NULL) {
printf("Error opening %s\n", argv[1]);
exit(EXIT_FAILU RE);
}
}
if (argc 2) {
out = fopen(argv[2], "w");
if (out == NULL) {
printf("Error opening %s\n", argv[2]);
exit(EXIT_FAILU RE);
}
}
start = clock();
catfilenobuff(i n, out);
end = clock();
printf("standar d cat took %f seconds\n", (end - start) * cps);
start = clock();
catfilebuffer(i n, out);
end = clock();
printf("big buffer cat took %f seconds\n", (end - start) * cps);
fflush(NULL);
return 0;
}- Hide quoted text -
- Show quoted text -
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void catfilebuffer(F ILE * in, FILE * out)
{
register int num_char;
>
setvbuf(in, NULL, _IOFBF, 1024 * 16);
setvbuf(out, NULL, _IOFBF, 1024 * 16);
>
/* Get characters */
while ((num_char = getc(in)) != EOF) {
/* Print to standard output */
putc(num_char, out);
}
>
}
>
void catfilenobuff(F ILE * in, FILE * out)
{
register int num_char;
>
/* Get characters */
while ((num_char = getc(in)) != EOF) {
/* Print to standard output */
putc(num_char, out);
}
>
}
>
int main(int argc, char **argv)
{
FILE *in = stdin;
FILE *out = stdout;
clock_t start,
end;
static const double cps = 1.0 / CLOCKS_PER_SEC;
if (argc 1) {
in = fopen(argv[1], "r");
if (in == NULL) {
printf("Error opening %s\n", argv[1]);
exit(EXIT_FAILU RE);
}
}
if (argc 2) {
out = fopen(argv[2], "w");
if (out == NULL) {
printf("Error opening %s\n", argv[2]);
exit(EXIT_FAILU RE);
}
}
start = clock();
catfilenobuff(i n, out);
end = clock();
printf("standar d cat took %f seconds\n", (end - start) * cps);
rewind(in);
fclose(out);
if (argc 2) {
out = fopen(argv[2], "w");
if (out == NULL) {
printf("Error opening %s\n", argv[2]);
exit(EXIT_FAILU RE);
}
} else
out = stdout;
start = clock();
catfilebuffer(i n, out);
end = clock();
printf("big buffer cat took %f seconds\n", (end - start) * cps);
fflush(NULL);
return 0;}
>
/*
Not nearly so dramatic! ;-)
C:\tmp>cat dict.sql dict.out
standard cat took 1.968000 seconds
big buffer cat took 1.891000 seconds
*/- Hide quoted text -
>
- Show quoted text -
much more robust. But it does lend a lot of speed (probably due to
the much lower number of function calls and tests).
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static char string[1024 * 16];
void catfilebufferfg ets(FILE * in, FILE * out)
{
setvbuf(in, NULL, _IOFBF, 1024 * 16);
setvbuf(out, NULL, _IOFBF, 1024 * 16);
/* Get characters (ERROR PRONE: {what if string 16K}) */
while (fgets(string, sizeof string, in)) {
fputs(string, out);
}
}
void catfilebuffer(F ILE * in, FILE * out)
{
register int num_char;
setvbuf(in, NULL, _IOFBF, 1024 * 16);
setvbuf(out, NULL, _IOFBF, 1024 * 16);
/* Get characters */
while ((num_char = getc(in)) != EOF) {
/* Print to standard output */
putc(num_char, out);
}
}
void catfilenobuff(F ILE * in, FILE * out)
{
register int num_char;
/* Get characters */
while ((num_char = getc(in)) != EOF) {
/* Print to standard output */
putc(num_char, out);
}
}
int main(int argc, char **argv)
{
FILE *in = stdin;
FILE *out = stdout;
clock_t start,
end;
static const double cps = 1.0 / CLOCKS_PER_SEC;
if (argc 1) {
in = fopen(argv[1], "r");
if (in == NULL) {
printf("Error opening %s\n", argv[1]);
exit(EXIT_FAILU RE);
}
}
if (argc 2) {
out = fopen(argv[2], "w");
if (out == NULL) {
printf("Error opening %s\n", argv[2]);
exit(EXIT_FAILU RE);
}
}
start = clock();
catfilenobuff(i n, out);
end = clock();
printf("standar d cat took %f seconds\n", (end - start) * cps);
rewind(in);
fclose(out);
if (argc 2) {
out = fopen(argv[2], "w");
if (out == NULL) {
printf("Error opening %s\n", argv[2]);
exit(EXIT_FAILU RE);
}
} else
out = stdout;
start = clock();
catfilebuffer(i n, out);
end = clock();
printf("big buffer cat took %f seconds\n", (end - start) * cps);
rewind(in);
fclose(out);
if (argc 2) {
out = fopen(argv[2], "w");
if (out == NULL) {
printf("Error opening %s\n", argv[2]);
exit(EXIT_FAILU RE);
}
} else
out = stdout;
start = clock();
catfilebufferfg ets(in, out);
end = clock();
printf("big buffer cat using fgets took %f seconds\n", (end -
start) * cps);
fflush(NULL);
return 0;
}
/*
C:\tmp>cat dict.sql dict.out
standard cat took 2.062000 seconds
big buffer cat took 2.016000 seconds
big buffer cat using fgets took 0.203000 seconds
*/
Comment