LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-04-2009, 10:29 AM   #16
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292

Here's your program, dunno if you're gonna use it, but it was a nice exercise for me, as I'm not particularly good at C coding:

PHP Code:
// calculates min and max for input file
// not much input checking is done, so use this program wisely, don't throw it crap or it will give you crap

#include <stdio.h>
#include <stdlib.h>

int main(int argcchar *argv[])
{
    
FILE file;
    
int nummaxmin;
    
    
// make sure we have only one input parameter, this includes the name of the program run, thus 2
    
if ( != argc )
    {
        
fputs ("ERROR: Need exactly 1 argument !\n",stderr);
        
printf("Usage:\t minmax file\n");
        exit(
1);
    }
    
    
// open file for reading
    
file fopen argv[1] , "r" );
    if ( 
file == NULL ) { fputs ("ERROR: failed to open file !\n",stderr); exit (1); }
    
    
// get num from file
    
fscanf (file"%d", &num);
    
    
// set starting values
    
min max num;
    
    
// read numbers until EOF, setting min and max as we go
    
while ( !feof(file) )
    {
        if ( 
num min )
        {
            
min num;
        }
        else if ( 
num max )
        {
            
max num;
        }
        
fscanf (file"%d", &num);
    }
    
    
// print min and max
    
printf("min=\t%d\n"min);
    
printf("max=\t%d\n"max);
    
    
// close file
    
fclose (file);
    
    return 
0;

Code:
bash-3.1$ for i in $(seq 100000); do echo $RANDOM >> file; done  
bash-3.1$ time awk 'NR==1{                                             
>  tempmin=$0
>  tempmax=$0
> }
> $0 >= tempmax{   tempmax=$0 }
> $0 <= tempmin {  tempmin = $0 }
> END{
>   print "min: "tempmin
>   print "max: "tempmax
> }' file
min: 0
max: 32767

real	0m0.081s
user	0m0.080s
sys	0m0.001s
bash-3.1$ time sort -n file > sorted && head -n1 sorted && tail -n1 sorted

real	0m0.129s
user	0m0.083s
sys	0m0.004s
0
32767
bash-3.1$ gcc minmax.c -o minmax
bash-3.1$ time ./minmax file
min=	0
max=	32767

real	0m0.023s
user	0m0.022s
sys	0m0.001s
# and if you wanted to be a real geek like me, proceed:
bash-3.1$ echo $CFLAGS
-march=nocona -O2 -pipe -fPIC
bash-3.1$ gcc minmax.c -march=nocona -O2 -pipe -fPIC -o minmax
bash-3.1$ strip --strip-unneeded minmax
bash-3.1$ time ./minmax file
min=	0
max=	32767

real	0m0.020s
user	0m0.019s
sys	0m0.001s
More benchmarking with higher numbers:

Code:
bash-3.1$ for i in $(seq 1000000); do echo $RANDOM >> file; done  
bash-3.1$ time awk 'NR==1{                                             
 tempmin=$0
 tempmax=$0
}
$0 >= tempmax{   tempmax=$0 }
$0 <= tempmin {  tempmin = $0 }
END{
  print "min: "tempmin
  print "max: "tempmax
}' file
min: 0
max: 32767

real	0m0.812s
user	0m0.806s
sys	0m0.006s
bash-3.1$ time sort -n file > sorted && head -n1 sorted && tail -n1 sorted

real	0m1.156s
user	0m1.124s
sys	0m0.032s
0
32767
bash-3.1$ time ./minmax file
min=	0
max=	32767

real	0m0.194s
user	0m0.187s
sys	0m0.006s
Code:
bash-3.1$ for i in $(seq 10000000); do echo $RANDOM >> file; done  
bash-3.1$ time awk 'NR==1{                                             
 tempmin=$0
 tempmax=$0
}
$0 >= tempmax{   tempmax=$0 }
$0 <= tempmin {  tempmin = $0 }
END{
  print "min: "tempmin
  print "max: "tempmax
}' file
min: 0
max: 32767

real	0m8.320s
user	0m8.265s
sys	0m0.053s
bash-3.1$ time sort -n file > sorted && head -n1 sorted && tail -n1 sorted

real	0m16.333s
user	0m14.540s
sys	0m0.284s
0
32767
bash-3.1$ time ./minmax file
min=	0
max=	32767

real	0m1.889s
user	0m1.836s
sys	0m0.052s
as you can see the differences become quite large with larger amounts of data, I didn't do it with more data because it was taking a while ...

Last edited by H_TeXMeX_H; 07-04-2009 at 10:35 AM.
 
Old 07-04-2009, 11:23 AM   #17
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
as minmax is a compiled c program that does a specific job, whereas with awk, the statements are being interpreted by the awk executable, there's of course a speed difference, no doubt about that.
 
Old 07-04-2009, 12:26 PM   #18
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292Reputation: 1292
Actually awk is surprisingly efficient. There is a constant 120 % or so difference between awk and C.

The sorting method has a great increase probably because the efficiency of the algorithm is often exponentially dependent on the number of values. So yeah, sorting is less efficient.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
LFS6.3 - Ch5.4.1 "/bin/sh sort not found" error at "make bootstrap" ubyt3m3 Linux From Scratch 2 06-23-2008 12:09 AM
How to make newer "tail" behave like older "tail" rylan76 Linux - Software 4 12-07-2007 04:27 AM
where can i find the paper "efficient dispersal of information for security.. " xzfxzf Programming 2 12-12-2005 03:59 AM
Howto end the "tail" command??? Schmurff Linux - Newbie 5 03-03-2004 05:32 AM
Error in Sendmail Tail : "apache set sender to <> using -f" cartfanatic39 Linux - Software 0 01-30-2004 10:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:28 PM.

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