Optimizing for a Speed-up


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Optimizing for a Speed-up
# 1  
Old 04-07-2006
Optimizing for a Speed-up

How would one go about optimizing this current .sh program so it works at a more minimal time. Such as is there a better way to count what I need than what I have done or better way to match patterns in the file? Thanks,

Code:
#declare variables to be used.
help=-1
count=0
JanCount=0
FebCount=0
MarCount=0
AprCount=0
MayCount=0
JunCount=0
JulCount=0
AugCount=0
SepCount=0
OctCount=0
NovCount=0
DecCount=0

# process all arguments (loop while $1 is present)
while [ -n "$1" ] ; do

 case $1 in
  -h*|-H*)		echo "Enter in a Year or a Month and Year to count the number of 

accesses made during that period." ; help=0 ;
			shift ;;

  [12][0-9][0-9][0-9])	the_year=$1 ;
			shift ;;

  Jan*|jan*|JAN*)      	the_month="Jan" ; shift ;;
  Feb*|feb*|FEB*)      	the_month="Feb" ; shift ;;
  Mar*|mar*|MAR*)      	the_month="Mar" ; shift ;;
  Apr*|apr*|APR*)      	the_month="Apr" ; shift ;;
  May*|may*|MAY*)      	the_month="May" ; shift ;;
  Jun*|jun*|JUN*)      	the_month="Jun" ; shift ;;
  Jul*|jul*|JUL*)      	the_month="Jul" ; shift ;;
  Aug*|aug*|AUG*)      	the_month="Aug" ; shift ;;
  Sep*|sep*|SEP*)      	the_month="Sep" ; shift ;;
  Oct*|oct*|OCT*)      	the_month="Oct" ; shift ;;
  Nov*|nov*|NOV*)      	the_month="Nov" ; shift ;;
  Dec*|dec*|DEC*)      	the_month="Dec" ; shift ;;

  *)			echo "? unrecognised input: $1" ;
			shift ;;
 esac
 done

# read the file to count accesses made
oldIFS="$IFS"
while IFS="/:" read day month year rest
    do
        MONTH=$month
	YEAR=$year

	if [ "$the_year" == "$year" ] &&  [ "$the_month" == "$month" ]  
	    then count=`expr $count + 1`
	elif [ "$the_year" == "$year" ] && [ "$the_month" == "" ]
	    then if [ "$month" = "Jan" ]
			then JanCount=`expr $JanCount + 1`
		 elif [ "$month" = "Feb" ]
			then FebCount=`expr $FebCount + 1`
		 elif [ "$month" = "Mar" ]
			then MarCount=`expr $MarCount + 1`
		 elif [ "$month" = "Apr" ]
			then AprCount=`expr $AprCount + 1`
		 elif [ "$month" = "May" ]
			then MayCount=`expr $MayCount + 1`
		 elif [ "$month" = "Jun" ]
			then JunCount=`expr $JunCount + 1`
		 elif [ "$month" = "Jul" ]
			then JulCount=`expr $JulCount + 1`
		 elif [ "$month" = "Aug" ]
			then AugCount=`expr $AugCount + 1`
		 elif [ "$month" = "Sep" ]
			then SepCount=`expr $SepCount + 1`
		 elif [ "$month" = "Oct" ]
			then OctCount=`expr $OctCount + 1`
		 elif [ "$month" = "Nov" ]
			then NovCount=`expr $NovCount + 1`
		 elif [ "$month" = "Dec" ]
			then DecCount=`expr $DecCount + 1`
		 fi
        fi
    done < my_sample_log2.txt
IFS=$oldIFS

# print out the accesses
if [ "$help" != 0 ]
	then if [ "$count" != "0" ]
			then echo "Report:" $count "accesses in $the_month $the_year."
		else
			echo "Report for the year $the_year:"
 			echo "Jan:" $JanCount "accesses."
			echo "Feb:" $FebCount "accesses."
			echo "Mar:" $MarCount "accesses."
			echo "Apr:" $AprCount "accesses."
			echo "May:" $MayCount "accesses."
			echo "Jun:" $JunCount "accesses."
			echo "Jul:" $JulCount "accesses."
			echo "Aug:" $AugCount "accesses."
			echo "Sep:" $SepCount "accesses."
			echo "Oct:" $OctCount "accesses."
			echo "Nov:" $NovCount "accesses."
			echo "Dec:" $DecCount "accesses."
	     fi
fi

# 2  
Old 04-07-2006
What shell are you using on which release of what OS running on what type of computer?
# 3  
Old 04-07-2006
FWIW - if you have a port of gcc or are on Linux you can compile shell scripts into a binary executable. This often gives a big improvement in speed, but undoes the primary reason for writing in shell in the first place - rapid, easy development without compiles.

See: http://directory.fsf.org/all/shc.html
# 4  
Old 04-07-2006
I doing the program in SH.
I know there are utilities such as grep for pattern matching, wc for counting and cut for substring extraction. I know my question was a bit hazy but I've tried changing the code to use these such utilities for a more short/efficient code and thus reducing the interpretor time. Any suggestions on how to do so would be appreciated.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Optimizing JS and CSS

Yes. Got few suggestions. - How about minifying resources - mod_expires - Service workers setup https://www.unix.com/attachments/web-programming/7709d1550557731-sneak-preview-new-unix-com-usercp-vuejs-demo-screenshot-png (8 Replies)
Discussion started by: Akshay Hegde
8 Replies

2. Debian

Optimizing exim performance

Hi, Recently, I experienced that exim was slow in sending outgoing mail, it was spending a lot of time in the queue, resulting in customer complains. I came across an article in the internet to optimize the performance of exim in the server. However, the location of the exim.conf is not in... (0 Replies)
Discussion started by: anaigini45
0 Replies

3. Shell Programming and Scripting

Optimizing bash script

any way the following code can be optimized? FIRSTIN=$( HKIPP=$(echo ${TMFR} | egrep -v "mo|MO|Mo" | egrep "m |M ") HRAMH=$(echo ${TMFR} | egrep "h|H") HRAMD=$(echo ${TMFR} | egrep "d|D") HRAMW=$(echo ${TMFR} | egrep "w|W") HKIPPO=$(echo ${TMFR} |... (5 Replies)
Discussion started by: SkySmart
5 Replies

4. Shell Programming and Scripting

Optimizing search using grep

I have a huge log file close to 3GB in size. My task is to generate some reporting based on # of times something is being logged. I need to find the number of time StringA , StringB , StringC is being called separately. What I am doing right now is: grep "StringA" server.log | wc -l... (4 Replies)
Discussion started by: Junaid Subhani
4 Replies

5. Shell Programming and Scripting

Optimizing awk script

Can this awk statement be optimized? i ask because log.txt is a giant file with several hundred thousands of lines of records. myscript.sh: while read line do searchterm="${1}" datecurr=$(date +%s) file=$(awk 'BEGIN{split(ARGV,var,",");print var}' $line) ... (3 Replies)
Discussion started by: SkySmart
3 Replies

6. Shell Programming and Scripting

Optimizing the code

Hi, I have two files in the format listed below. I need to find out all values from field 12 to field 20 present in file 2 and list them in file3(format as file2) File1 : FEIN,CHRISTA... (2 Replies)
Discussion started by: nua7
2 Replies

7. Filesystems, Disks and Memory

data from blktrace: read speed V.S. write speed

I analysed disk performance with blktrace and get some data: read: 8,3 4 2141 2.882115217 3342 Q R 195732187 + 32 8,3 4 2142 2.882116411 3342 G R 195732187 + 32 8,3 4 2144 2.882117647 3342 I R 195732187 + 32 8,3 4 2145 ... (1 Reply)
Discussion started by: W.C.C
1 Replies

8. OS X (Apple)

Optimizing OSX

Hi forum, I'm administrating a workstation/server for my lab and I was wondering how to optimize OSX. I was wondering what unnecessary background tasks I could kick off the system so I free up as much memory and cpu power. Other optimization tips are also welcome (HD parameters, memory... (2 Replies)
Discussion started by: deiphon
2 Replies

9. UNIX and Linux Applications

Optimizing query

Hi All, My first thread to this sub-forum and first thread of this sub-forum :) Here it is, Am trying to delete duplicates from a table retaining just 1 duplicate value out of the duplicate records for example : from n records of a table out of which x are duplicates, I want to remove x... (15 Replies)
Discussion started by: matrixmadhan
15 Replies

10. Filesystems, Disks and Memory

dmidecode, RAM speed = "Current Speed: Unknown"

Hello, I have a Supermicro server with a P4SCI mother board running Debian Sarge 3.1. This is the "dmidecode" output related to RAM info: RAM speed information is incomplete.. "Current Speed: Unknown", is there anyway/soft to get the speed of installed RAM modules? thanks!! Regards :)... (0 Replies)
Discussion started by: Santi
0 Replies
Login or Register to Ask a Question