The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 02-11-2009
csmklee csmklee is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 3
calculate diff in csv

I wrote a func to calculcate integers difference in csv.

getSegLatency() {
latencies=0$1
E2E=0`echo $2 | sed 's/\.000000//g'`
integer segLatency=0
set -A arr `echo $latencies | sed 's/\.000000//g' | sed 's/,/ /g'`
res=${arr[0]}
integer i=1
while ((i < ${#arr[*]})); do
((segLatency=${arr[$i]} - ${arr[$i - 1]}));
res="$res,$segLatency"
(( i = i + 1));
done
((segLatency=$E2E - ${arr[$i - 1]}));
res="$res,$segLatency"
echo $res
}

in which the number of elements in $1 may vary
e.g. when i call like:
getSegLatency 1.000000,4.000000,6.000000,7.000000 11.000000
i'll get:
3,2,1,4

It works well but the performance is slow.

Is there any way to polish the script to run faster?