Skip to content

Commit

Permalink
use shell arithmetic expansion
Browse files Browse the repository at this point in the history
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
peff committed Nov 15, 2011
1 parent 424194d commit 2243964
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions spark
Expand Up @@ -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
}

Expand All @@ -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
Expand Down

0 comments on commit 2243964

Please sign in to comment.