help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] performance difference between math operations


From: Peng Yu
Subject: [Help-bash] performance difference between math operations
Date: Wed, 12 Dec 2018 15:36:50 -0600

Hi,

I saw the following performance differences among equivalent math
operations. I ordered them based on their speed. However, the actual
runtime numbers fluctuated. So I am not completely sure the order is
correct. Therefore, knowing the underlying operations may help with
the correct ordering.

What are the underlying machine operations used in each case that can
help determine their performance order?

Can the rest cases be made with performance matching the first case? Thanks.

$ cat main.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

declare -i i

set -v
declare -i n=300000
declare -i x1
time for ((i=0;i<n;++i))
do
    x1+=1
done

declare -i x2
time for ((i=0;i<n;++i))
do
    x2=x2+1
done

declare y1
time for ((i=0;i<n;++i))
do
    ((++y1))
done

declare y2
time for ((i=0;i<n;++i))
do
    ((y2+=1))
done

declare y3
time for ((i=0;i<n;++i))
do
    ((y3=y3+1))
done

$  ./main.sh
declare -i n=300000
declare -i x1
time for ((i=0;i<n;++i))
do
    x1+=1
done

real    0m3.192s
user    0m3.087s
sys    0m0.095s

declare -i x2
time for ((i=0;i<n;++i))
do
    x2=x2+1
done

real    0m3.424s
user    0m3.330s
sys    0m0.088s

declare y1
time for ((i=0;i<n;++i))
do
    ((++y1))
done

real    0m3.459s
user    0m3.363s
sys    0m0.092s

declare y2
time for ((i=0;i<n;++i))
do
    ((y2+=1))
done

real    0m3.632s
user    0m3.526s
sys    0m0.100s

declare y3
time for ((i=0;i<n;++i))
do
    ((y3=y3+1))
done

real    0m3.865s
user    0m3.758s
sys    0m0.100s


-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]