Permalink
Browse files

use shell arithmetic expansion

This is way faster than invoking bc repeatedly.

Before:

  $ DATA=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
  $ time spark.old $DATA >/dev/null
  real    0m1.018s
  user    0m0.020s
  sys     0m0.060s

After:

  $ time spark $DATA >/dev/null
  real    0m0.089s
  user    0m0.000s
  sys     0m0.008s

Or to make it more clear:

  $ elapsed_ms() {
      /usr/bin/time -f %e "$@" 2>&1 >/dev/null |
        perl -lpe '$_ *= 1000'
    }
  $ spark "$(elapsed_ms spark.old $DATA),$(elapsed_ms spark $DATA)"
  ▇▁
  • Loading branch information...
1 parent 424194d commit 22439644fb39de086482d71d36a9251966affc67 @peff peff committed Nov 15, 2011
Showing with 4 additions and 4 deletions.
  1. +4 −4 spark
View
@@ -83,7 +83,7 @@ sort_min()
tier()
{
number_of_ticks=${#ticks[@]}
- distance=$(echo "$(sort_max) / $number_of_ticks" | bc)
+ distance=$(( $(sort_max) / $number_of_ticks ))
echo $distance
}
@@ -98,9 +98,9 @@ print_tick()
do
tick=${ticks[$i]}
number=$1
- less_than=$(echo "$i * $tier + sort_min + $tier" | bc)
- greater_than=$(echo "($i - 1) * $tier + sort_min + $tier" | bc)
- result=$(echo "$number <= $less_than && $number >= $greater_than" | bc)
+ less_than=$(( $i * $tier + sort_min + $tier ))
+ greater_than=$(( ($i - 1) * $tier + sort_min + $tier ))
+ result=$(( $number <= $less_than && $number >= $greater_than ))
if [ $result -eq 1 ]
then

0 comments on commit 2243964

Please sign in to comment.