Rick Cogley's Tech Logr

Short Technical Laser Bursts %%

RC Logr 20190315 082448

Friday, 15 Mar, 2019

If you tried using bc to do the quick pi calculation I mentioned yesterday, you can also use an internal value SECONDS to calculate elapsed seconds. The SECONDS only tracks whole numbers, so the time calculation is rather rough, but it works in a pinch.

This works in zsh or bash: SECONDS=0 ; echo "scale=3000; 4*a(1)" | bc -l ; echo $SECONDS πŸ€“

This also works, using simple math to do date arithmetic (the $(()) triggers the math):

1
STARTT=$(date +%s) ; echo "scale=2000; 4*a(1)" | bc -l ; ENDT=$(date +%s) ; echo "Took $(($ENDT - $STARTT)) seconds to calculate this..."

Or in zsh, this gives output like 1.92s user 0.02s system 99% cpu 1.954 total:

1
{ time ( echo "scale=2000; 4*a(1)" | bc -l ; ) } 2>&1 

In bash, just replace the () around the command you are timing with {}. Calculating pi using this 4*atan(1) formula is super slow, so anything more than a couple thousand digits will take tiiimeeee.

RC Logr 20190315 082448 - If you tried using bc to do … Rick Cogley
Back to Home