Fastest FizzBuzz
I tried python, shell and awk script to check the fastest FizzBuzz program
# Program prints no. 1 to 100
# For multiples of 3: Fizz, 5: Buzz, both 3 & 5 : FizzBuzz
Shell Script
$ time ./fizzbuzz.sh > /dev/null
real 0m0.394s
user 0m0.249s
sys 0m0.146s
Python Script
$ time ./fizzbuzz.py > /dev/null
real 0m0.072s
user 0m0.065s
sys 0m0.004s
AWK Script
$ time awk -f ./fizzbuzz.awk > /dev/null
real 0m0.007s
user 0m0.006s
sys 0m0.000s
Sharing fastest script in awk.
$ cat fizzbuzz.awk
BEGIN {
for (i=1; i<=100; i++) {
db3=i%3;db5=i%5;
if (db3==0 && db5==0) {
print "FizzBuzz"
} else if (db3==0) {
print "Fizz"
} else if (db5==0) {
print "Buzz"
} else print i}
}
No comments:
Post a Comment