Guest User

Including binary files in C -- compilation benchmark

a guest
Apr 16th, 2020
273
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. avg_time() {
  4.     #
  5.     # usage: avg_time n command ...
  6.     #
  7.     n=$1; shift
  8.     (($# > 0)) || return                   # bail if no command given
  9.     for ((i = 0; i < n; i++)); do
  10.         { time -p "$@" &>/dev/null; } 2>&1 # ignore the output of the command
  11.                                            # but collect time's output in stdout
  12.     done | awk '
  13.        /real/ { real = real + $2; nr++ }
  14.        /user/ { user = user + $2; nu++ }
  15.        /sys/  { sys  = sys  + $2; ns++}
  16.        END    {
  17.                 if (nr>0) printf("%f,%f,%f\n", real/nr, user/nr, sys/nr);
  18.               }'
  19. }
  20.  
  21. # Generate base file
  22. dd if=/dev/random of=base.bin bs=1m count=1000 >& /dev/null
  23.  
  24. # Number of trials per file for compilation
  25. TRIALS=3
  26.  
  27. for i in `seq 1 50`; do
  28.     # Copy small subsection
  29.     dd if=base.bin of=random.bin bs=1m count="$i" >& /dev/null
  30.     # Convert it to a C file and append it to the template
  31.     xxd -i random.bin | cat - template.c > benchmark.c
  32.     printf "$i,"
  33.     avg_time $TRIALS gcc -c benchmark.c
  34. done
  35.  
  36.  
  37. ## template.c
  38. # #include <stdio.h>
  39. # int main() {
  40. #     char mod = 0;
  41. #     for (int i=0; i<random_bin_len; ++i) {
  42. #         mod += random_bin[i];
  43. #     }
  44. #     printf("%d", mod);
  45. #     return 0;
  46. # }
RAW Paste Data