LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-05-2010, 08:30 PM   #1
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Rep: Reputation: 41
Question Which is faster in these fread() commands?


Which is faster:

fread(&buf, 1, 1024, fp);

or

fread(&buf, 1024, 1, fp);

?

Regards,
archieval
 
Old 05-05-2010, 08:54 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by archieval View Post
Which is faster:

fread(&buf, 1, 1024, fp);

or

fread(&buf, 1024, 1, fp);

?

Regards,
archieval
Probably the one with greater item size, but why won't you check it yourself ?
 
Old 05-05-2010, 08:58 PM   #3
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
You can use time command as below.

Code:
time fread(&buf, 1, 1024, fp);

Last edited by vikas027; 05-05-2010 at 08:59 PM.
 
Old 05-05-2010, 08:59 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by vikas027 View Post
You can use time command as below.

Code:
time fread(&buf, 1, 1024, fp);
Do you think your suggestion is a valid "C" code ?
 
Old 05-05-2010, 09:34 PM   #5
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Original Poster
Rep: Reputation: 41
time is a unix system command. I used the codes below to test but I got seg fault...

Code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

int main(void)
{
        FILE *fp;
        char *buf;
        unsigned long t1, t2;
        struct tm tm1, tm2;
        struct timeval tb1, tb2, tb3, tb4;

        fp = fopen("../hi_archie/dvr_test.c", "r");
        if (fp == NULL)
        {
                printf("error\n");
                return 1;
        }
        
        buf = malloc(2048);
        if(buf == NULL)
                return;
        printf("xxx\n");

        gettimeofday(&tb1, NULL);
        fread(&buf, 1024, 1, fp);
        gettimeofday(&tb2, NULL);

        printf("time lapsed %lu\n", tb2.tv_usec - tb1.tv_usec);

        fseek(fp, 0, SEEK_SET);
        gettimeofday(&tb3, NULL);
        fread(&buf, 1, 1024, fp);
        gettimeofday(&tb4, NULL);
        printf("time lapsed %lu\n", tb4.tv_usec - tb3.tv_usec);

        free(buf);

        return 0;
}

result:
$ gcc -Wall test.c ; ./a.out

test.c: In function ‘main’:
test.c:23: warning: ‘return’ with no value, in function returning non-void
test.c:11: warning: unused variable ‘tm2’
test.c:11: warning: unused variable ‘tm1’
test.c:10: warning: unused variable ‘t2’
test.c:10: warning: unused variable ‘t1’
time lapsed 18
Segmentation fault
 
Old 05-05-2010, 09:57 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by archieval View Post
time is a unix system command. I used the codes below to test but I got seg fault...

Code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

int main(void)
{
        FILE *fp;
        char *buf;
        unsigned long t1, t2;
        struct tm tm1, tm2;
        struct timeval tb1, tb2, tb3, tb4;

        fp = fopen("../hi_archie/dvr_test.c", "r");
        if (fp == NULL)
        {
                printf("error\n");
                return 1;
        }
        
        buf = malloc(2048);
        if(buf == NULL)
                return;
        printf("xxx\n");

        gettimeofday(&tb1, NULL);
        fread(&buf, 1024, 1, fp);
        gettimeofday(&tb2, NULL);

        printf("time lapsed %lu\n", tb2.tv_usec - tb1.tv_usec);

        fseek(fp, 0, SEEK_SET);
        gettimeofday(&tb3, NULL);
        fread(&buf, 1, 1024, fp);
        gettimeofday(&tb4, NULL);
        printf("time lapsed %lu\n", tb4.tv_usec - tb3.tv_usec);

        free(buf);

        return 0;
}

result:
$ gcc -Wall test.c ; ./a.out

test.c: In function ‘main’:
test.c:23: warning: ‘return’ with no value, in function returning non-void
test.c:11: warning: unused variable ‘tm2’
test.c:11: warning: unused variable ‘tm1’
test.c:10: warning: unused variable ‘t2’
test.c:10: warning: unused variable ‘t1’
time lapsed 18
Segmentation fault
So, first get rid of warnings, the determine the line causing segfault.

...

Use stderr, not stdout, for diagnostic output.

Last edited by Sergei Steshenko; 05-05-2010 at 10:00 PM.
 
Old 05-05-2010, 11:18 PM   #7
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Original Poster
Rep: Reputation: 41
wrong in dereferencing buf >___<

should be

fread(buf, 1024, 1, fp);
fread(buf, 1, 1024, fp);

its ok now.

time lapsed 30
time lapsed 6

so the second is the faster one.
 
Old 05-06-2010, 12:20 AM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by archieval View Post
wrong in dereferencing buf >___<

should be

fread(buf, 1024, 1, fp);
fread(buf, 1, 1024, fp);

its ok now.

time lapsed 30
time lapsed 6

so the second is the faster one.
I wouldn't be so sure - run your test a number of times.
 
Old 05-06-2010, 01:09 AM   #9
broken
Member
 
Registered: Apr 2010
Location: Your mom's trailer
Distribution: NetBSD
Posts: 31

Rep: Reputation: 15
How about using the "correct" one rather than the fastest one? Good luck handling the return value if you pick the incorrect version because it is faster.
 
Old 05-06-2010, 03:04 AM   #10
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Original Poster
Rep: Reputation: 41
[archieval@localhost hi_archie_empty]$ ./a.out
time lapsed 30
time lapsed 5
[archieval@localhost hi_archie_empty]$ ./a.out
time lapsed 32
time lapsed 5
[archieval@localhost hi_archie_empty]$ ./a.out
time lapsed 20
time lapsed 3
[archieval@localhost hi_archie_empty]$ ./a.out
time lapsed 30
time lapsed 5
[archieval@localhost hi_archie_empty]$ ./a.out
time lapsed 30
time lapsed 6
[archieval@localhost hi_archie_empty]$ ./a.out
time lapsed 28
time lapsed 5
[archieval@localhost hi_archie_empty]$ ./a.out
time lapsed 30
time lapsed 5
[archieval@localhost hi_archie_empty]$ ./a.out
time lapsed 30
time lapsed 6
[archieval@localhost hi_archie_empty]$ ./a.out
time lapsed 33
time lapsed 5
[archieval@localhost hi_archie_empty]$ ./a.out
time lapsed 31
time lapsed 6
[archieval@localhost hi_archie_empty]$ ./a.out
time lapsed 50
time lapsed 6
[archieval@localhost hi_archie_empty]$ ./a.out
time lapsed 35
time lapsed 6
[archieval@localhost hi_archie_empty]$ ./a.out
time lapsed 31
time lapsed 7
[archieval@localhost hi_archie_empty]$ ./a.out
time lapsed 43
time lapsed 5
[archieval@localhost hi_archie_empty]$ ./a.out
time lapsed 30
time lapsed 5
[archieval@localhost hi_archie_empty]$ ./a.out
time lapsed 32
time lapsed 6
[archieval@localhost hi_archie_empty]$ ./a.out
time lapsed 30
time lapsed 6
 
Old 05-06-2010, 03:14 AM   #11
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Original Poster
Rep: Reputation: 41
i changed the code:
Code:
        for (i = 0; i<100; i++)
        {
                gettimeofday(&tb1, NULL);
                fread(buf, 1024, 1, fp);
                gettimeofday(&tb2, NULL);

                printf("%lu, ", tb2.tv_usec - tb1.tv_usec);

                gettimeofday(&tb3, NULL);
                fread(buf, 1, 1024, fp);
                gettimeofday(&tb4, NULL);
                printf("%lu\n", tb4.tv_usec - tb3.tv_usec);

        }
result:

33, 1
1, 1
8, 1
2, 1
7, 2
1, 2
5, 1
1, 1
5, 1
1, 1
5, 1
1, 2
5, 1
1, 1
5, 1
2, 1
5, 1
1, 1
5, 2
2, 1
5, 1
1, 2
6, 2
1, 2
5, 2
2, 2
5, 1
1, 2
6, 1
1, 2
6, 1
1, 1
6, 1
2, 1
6, 1
1, 1
5, 1
2, 1
6, 2
1, 1
5, 2
1, 1
5, 1
1, 2
6, 1
1, 1
5, 1
1, 2
6, 1
2, 1
7, 1
2, 1
6, 1
1, 1
5, 2
1, 1
7, 2
1, 1
5, 2
2, 1
5, 1
1, 1
7, 2
1, 2
6, 1
1, 2
5, 2
2, 1
5, 1
2, 1
5, 2
2, 1
6, 2
2, 2
6, 1
1, 1
5, 1
1, 1
5, 2
1, 1
6, 2
1, 2
6, 1
2, 1
5, 2
1, 2
6, 2
2, 1
6, 2
2, 1
5, 1
2, 1
5, 1
1, 2
5, 1
1, 1
5, 2
2, 2
5, 1
1, 1
 
Old 05-06-2010, 10:57 AM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by archieval View Post
i changed the code:
Code:
        for (i = 0; i<100; i++)
        {
                gettimeofday(&tb1, NULL);
                fread(buf, 1024, 1, fp);
                gettimeofday(&tb2, NULL);

                printf("%lu, ", tb2.tv_usec - tb1.tv_usec);

                gettimeofday(&tb3, NULL);
                fread(buf, 1, 1024, fp);
                gettimeofday(&tb4, NULL);
                printf("%lu\n", tb4.tv_usec - tb3.tv_usec);

        }
result:

33, 1
1, 1
8, 1
2, 1
7, 2
1, 2
5, 1
1, 1
5, 1
1, 1
5, 1
1, 2
5, 1
1, 1
5, 1
2, 1
5, 1
1, 1
5, 2
2, 1
5, 1
1, 2
6, 2
1, 2
5, 2
2, 2
5, 1
1, 2
6, 1
1, 2
6, 1
1, 1
6, 1
2, 1
6, 1
1, 1
5, 1
2, 1
6, 2
1, 1
5, 2
1, 1
5, 1
1, 2
6, 1
1, 1
5, 1
1, 2
6, 1
2, 1
7, 1
2, 1
6, 1
1, 1
5, 2
1, 1
7, 2
1, 1
5, 2
2, 1
5, 1
1, 1
7, 2
1, 2
6, 1
1, 2
5, 2
2, 1
5, 1
2, 1
5, 2
2, 1
6, 2
2, 2
6, 1
1, 1
5, 1
1, 1
5, 2
1, 1
6, 2
1, 2
6, 1
2, 1
5, 2
1, 2
6, 2
2, 1
6, 2
2, 1
5, 1
2, 1
5, 1
1, 2
5, 1
1, 1
5, 2
2, 2
5, 1
1, 1
Now create two test programs - in one you'll have this order:

fread(buf, 1024, 1, fp);
fread(buf, 1, 1024, fp);

and in the other the opposite order:

fread(buf, 1, 1024, fp);
fread(buf, 1024, 1, fp);
.

And compare results. By the way, you already have quite a lot of food for thought.
 
Old 05-06-2010, 08:37 PM   #13
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Original Poster
Rep: Reputation: 41
Code:
                gettimeofday(&tb1, NULL);
                fread(buf, 1, 1024, fp);
                gettimeofday(&tb2, NULL);

                printf("%lu, ", tb2.tv_usec - tb1.tv_usec);

                gettimeofday(&tb3, NULL);
                fread(buf, 1024, 1, fp);
                gettimeofday(&tb4, NULL);
                printf("%lu\n", tb4.tv_usec - tb3.tv_usec);
here is the result:

there is some kind of overhead on the first call or at the start of the loop...

Code:
29, 1
1, 1
7, 1
1, 1
6, 1
1, 2
6, 1
1, 1
6, 1
1, 1
5, 1
1, 1
6, 1
1, 1
6, 1
1, 1
4, 1
1, 1
6, 2
1, 1
5, 2
2, 1
5, 1
1, 1
6, 1
1, 1
6, 1
1, 1
5, 1
1, 2
5, 1
2, 2
6, 1
1, 1
5, 1
1, 1
5, 1
1, 2
6, 1
1, 2
6, 1
1, 2
6, 2
1, 1
5, 2
2, 1
5, 2
2, 1
6, 2
1, 1
5, 1
1, 1
5, 1
1, 2
5, 1
1, 2
6, 1
2, 1
6, 1
1, 1
7, 1
1, 1
5, 1
1, 2
5, 1
1, 1
6, 1
1, 2
5, 1
2, 1
6, 2
1, 1
5, 1
1, 1
6, 1
1, 2
5, 2
2, 1
6, 1
1, 1
5, 1
1, 2
5, 1
1, 1
6, 1
1, 1
6, 1
1, 1
6, 1
1, 1
7, 1
1, 1
6, 2
2, 1
6, 2
2, 1
5, 1
1, 1
6, 1
2, 1
 
Old 05-06-2010, 08:48 PM   #14
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by archieval View Post
Code:
                gettimeofday(&tb1, NULL);
                fread(buf, 1, 1024, fp);
                gettimeofday(&tb2, NULL);

                printf("%lu, ", tb2.tv_usec - tb1.tv_usec);

                gettimeofday(&tb3, NULL);
                fread(buf, 1024, 1, fp);
                gettimeofday(&tb4, NULL);
                printf("%lu\n", tb4.tv_usec - tb3.tv_usec);
here is the result:

there is some kind of overhead on the first call or at the start of the loop...

Code:
29, 1
1, 1
7, 1
1, 1
6, 1
1, 2
6, 1
1, 1
6, 1
1, 1
5, 1
1, 1
6, 1
1, 1
6, 1
1, 1
4, 1
1, 1
6, 2
1, 1
5, 2
2, 1
5, 1
1, 1
6, 1
1, 1
6, 1
1, 1
5, 1
1, 2
5, 1
2, 2
6, 1
1, 1
5, 1
1, 1
5, 1
1, 2
6, 1
1, 2
6, 1
1, 2
6, 2
1, 1
5, 2
2, 1
5, 2
2, 1
6, 2
1, 1
5, 1
1, 1
5, 1
1, 2
5, 1
1, 2
6, 1
2, 1
6, 1
1, 1
7, 1
1, 1
5, 1
1, 2
5, 1
1, 1
6, 1
1, 2
5, 1
2, 1
6, 2
1, 1
5, 1
1, 1
6, 1
1, 2
5, 2
2, 1
6, 1
1, 1
5, 1
1, 2
5, 1
1, 1
6, 1
1, 1
6, 1
1, 1
6, 1
1, 1
7, 1
1, 1
6, 2
2, 1
6, 2
2, 1
5, 1
1, 1
6, 1
2, 1
So, seeing all this, do you realize your original question actually doesn't make sense ?
 
Old 05-06-2010, 11:59 PM   #15
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Original Poster
Rep: Reputation: 41
are you implying that they are of equal speed?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Linux 2.6.30 Gets Faster Boot - but is Fedora Faster? LXer Syndicated Linux News 0 06-11-2009 04:40 AM
LXer: Firefox 3.5 Speed Freak: Faster Development, Faster Performance LXer Syndicated Linux News 0 06-10-2009 02:42 AM
best way of using fread and fwrite rohanak Programming 1 05-02-2008 07:26 AM
LXer: Firefox 3 Beta 4 is 5x faster than IE7, 3x faster than FF2 LXer Syndicated Linux News 0 03-12-2008 05:50 PM
using fread with fscanf andystanfordjason Programming 4 12-20-2006 09:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration