Advertisement
Paul_Pedant

Order(N) Demo

Oct 20th, 2020
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.15 KB | None | 0 0
  1. #! /bin/bash
  2.  
  3. doSumAll () {
  4.  
  5.     local AWK='
  6. { for (j = 1; j <= $1; j++) sum += j; }
  7. END { printf ("doSumAll %s is %s\n", $1, sum); }
  8. '
  9.     echo "${1}" | awk -f <( printf '%s' "${AWK}" ) > /dev/null
  10. }
  11.  
  12. doFibonacci () {
  13.  
  14.     local AWK='
  15. BEGIN { a = 0; b = 1; }
  16. { for (j = 2; j <= $1; j++) { c = a + b; a = b; b = c; } }
  17. END { printf ("doFibonacci %s is %s\n", $1, b); }
  18. '
  19.     echo "${1}" | awk -f <( printf '%s' "${AWK}" ) > /dev/null
  20. }
  21.  
  22. doEvenOdd () {
  23.  
  24.     local AWK='
  25. { for (j = 1; j <= $1; j++) a = j % 2; }
  26. END { printf ("doEvenOdd %s is %s\n", $1, --j % 2); }
  27. '
  28.     echo "${1}" | awk -f <( printf '%s' "${AWK}" ) > /dev/null
  29. }
  30.  
  31. Real () { r="${1}"; r="${r#*real}"; r="${r%%user*}"; echo ${r}; }
  32.  
  33. Compare () {
  34.  
  35.     local N="${1}"
  36.  
  37.     TimeA="$( ( time doSumAll "${N}" ) 2>&1 )"
  38.     TimeB="$( ( time doFibonacci "${N}" ) 2>&1 )"
  39.     TimeC="$( ( time doEvenOdd "${N}" ) 2>&1 )"
  40.  
  41.     printf 'TimeA %s %s\n' "${N}" "$( Real "${TimeA}" )"
  42.     printf 'TimeB %s %s\n' "${N}" "$( Real "${TimeB}" )"
  43.     printf 'TimeC %s %s\n' "${N}" "$( Real "${TimeC}" )"
  44.     echo
  45. }
  46.  
  47.     for n in {1..6}000000; do
  48.         Compare "${n}"
  49.     done
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement