golang.org/x/exp@v0.0.0-20240506185415-9bf2ced13842/shootout/timing.sh (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2009 The Go Authors.  All rights reserved.
     3  # Use of this source code is governed by a BSD-style
     4  # license that can be found in the LICENSE file.
     5  
     6  set -e
     7  
     8  eval $(go tool dist env)
     9  GC="go tool compile"
    10  LD="go tool link"
    11  
    12  gccm=""
    13  case "$O" in
    14  8)
    15  	gccm=-m32;;
    16  6)
    17  	gccm=-m64;;
    18  esac
    19  
    20  EXE="out"
    21  havepcre=true
    22  haveglib=true
    23  havegmp=true
    24  case "$(uname)" in
    25  *MINGW* | *WIN32* | *CYGWIN*)
    26  	havepcre=false
    27  	haveglib=false
    28  	havegmp=false
    29  	if which pkg-config >/dev/null 2>&1; then
    30  		if pkg-config --cflags libpcre >/dev/null 2>&1
    31  		then
    32  			echo "havepcre"
    33  			havepcre=true
    34  		fi
    35  		if pkg-config --cflags glib-2.0 >/dev/null 2>&1
    36  		then
    37  			haveglib=true
    38  		fi
    39  		if pkg-config --cflags gmp >/dev/null 2>&1
    40  		then
    41  			havegmp=true
    42  		fi
    43  	fi
    44  	EXE=exe;;
    45  esac
    46  
    47  PATH=.:$PATH
    48  
    49  havegccgo=false
    50  if which gccgo >/dev/null 2>&1
    51  then
    52  	havegccgo=true
    53  fi
    54  
    55  mode=run
    56  case X"$1" in
    57  X-test)
    58  	mode=test
    59  	shift
    60  esac
    61  
    62  gc() {
    63  	$GC $1.go; $LD -o a.$EXE $1.o
    64  }
    65  
    66  gc_B() {
    67  	$GC -B $1.go; $LD -o a.$EXE $1.o
    68  }
    69  
    70  runonly() {
    71  	if [ $mode = run ]
    72  	then
    73  		"$@"
    74  	fi
    75  }
    76  
    77  run() {
    78  	if [ $mode = test ]
    79  	then
    80  		if echo $1 | grep -q '^gc '
    81  		then
    82  			$1	# compile the program
    83  			program=$(echo $1 | sed 's/gc //')
    84  			shift
    85  			echo $program
    86  			$1 <fasta-1000.txt > /tmp/$$
    87  			case $program in
    88  			chameneosredux)
    89  				# exact numbers may vary but non-numbers should match
    90  				grep -v '[0-9]' /tmp/$$ > /tmp/$$x
    91  				grep -v '[0-9]' chameneosredux.txt > /tmp/$$y
    92  				cmp /tmp/$$x /tmp/$$y
    93  				rm -f /tmp/$$ /tmp/$$x /tmp/$$y
    94  				;;
    95  			*)
    96  				cmp /tmp/$$ $program.txt
    97  				rm -f /tmp/$$
    98  			esac
    99  		fi
   100  		return
   101  	fi
   102  	if ! $havegccgo && echo $1 | grep -q '^gccgo '
   103  	then
   104  		return
   105  	fi
   106  	echo -n '	'$1'	'
   107  	$1
   108  	shift
   109  	
   110  	echo $((time -p $* >/dev/null) 2>&1) | awk '{print $4 "u " $6 "s " $2 "r"}'
   111  }
   112  
   113  fasta() {
   114  	runonly echo 'fasta -n 25000000'
   115  	run "gcc $gccm -O2 fasta.c" a.$EXE 25000000
   116  	run 'gccgo -O2 fasta.go' a.$EXE -n 25000000	#commented out until WriteString is in bufio
   117  	run 'gc fasta' a.$EXE -n 25000000
   118  	run 'gc_B fasta' a.$EXE -n 25000000
   119  }
   120  
   121  revcomp() {
   122  	runonly gcc -O2 fasta.c
   123  	runonly a.$EXE 25000000 > x
   124  	runonly echo 'reverse-complement < output-of-fasta-25000000'
   125  	run "gcc $gccm -O2 reverse-complement.c" a.$EXE < x
   126  	run 'gccgo -O2 reverse-complement.go' a.$EXE < x
   127  	run 'gc reverse-complement' a.$EXE < x
   128  	run 'gc_B reverse-complement' a.$EXE < x
   129  	rm x
   130  }
   131  
   132  nbody() {
   133  	runonly echo 'nbody -n 50000000'
   134  	run "gcc $gccm -O2 nbody.c -lm" a.$EXE 50000000
   135  	run 'gccgo -O2 nbody.go' a.$EXE -n 50000000
   136  	run 'gc nbody' a.$EXE -n 50000000
   137  	run 'gc_B nbody' a.$EXE -n 50000000
   138  }
   139  
   140  binarytree() {
   141  	runonly echo 'binary-tree 15 # too slow to use 20'
   142  	run "gcc $gccm -O2 binary-tree.c -lm" a.$EXE 15
   143  	run 'gccgo -O2 binary-tree.go' a.$EXE -n 15
   144  	run 'gccgo -O2 binary-tree-freelist.go' a.$EXE -n 15
   145  	run 'gc binary-tree' a.$EXE -n 15
   146  	run 'gc binary-tree-freelist' a.$EXE -n 15
   147  }
   148  
   149  fannkuch() {
   150  	runonly echo 'fannkuch 12'
   151  	run "gcc $gccm -O2 fannkuch.c" a.$EXE 12
   152  	run 'gccgo -O2 fannkuch.go' a.$EXE -n 12
   153  	run 'gccgo -O2 fannkuch-parallel.go' a.$EXE -n 12
   154  	run 'gc fannkuch' a.$EXE -n 12
   155  	run 'gc fannkuch-parallel' a.$EXE -n 12
   156  	run 'gc_B fannkuch' a.$EXE -n 12
   157  }
   158  
   159  regexdna() {
   160  	runonly gcc -O2 fasta.c
   161  	runonly a.$EXE 100000 > x
   162  	runonly echo 'regex-dna 100000'
   163  	if  $havepcre; then
   164  		run "gcc $gccm -O2 regex-dna.c $(pkg-config libpcre --cflags --libs)" a.$EXE <x
   165  	fi
   166  	run 'gccgo -O2 regex-dna.go' a.$EXE <x
   167  	run 'gccgo -O2 regex-dna-parallel.go' a.$EXE <x
   168  	run 'gc regex-dna' a.$EXE <x
   169  	run 'gc regex-dna-parallel' a.$EXE <x
   170  	run 'gc_B regex-dna' a.$EXE <x
   171  	rm x
   172  }
   173  
   174  spectralnorm() {
   175  	runonly echo 'spectral-norm 5500'
   176  	run "gcc $gccm -O2 spectral-norm.c -lm" a.$EXE 5500
   177  	run 'gccgo -O2 spectral-norm.go' a.$EXE -n 5500
   178  	run 'gc spectral-norm' a.$EXE -n 5500
   179  	run 'gc_B spectral-norm' a.$EXE -n 5500
   180  }
   181  
   182  knucleotide() {
   183  	runonly gcc -O2 fasta.c
   184  	runonly a.$EXE 1000000 > x  # should be using 25000000
   185  	runonly echo 'k-nucleotide 1000000'
   186  	if [ $mode = run ] && $haveglib; then
   187  		run "gcc -O2 k-nucleotide.c $(pkg-config glib-2.0 --cflags --libs)" a.$EXE <x
   188  	fi
   189  	run 'gccgo -O2 k-nucleotide.go' a.$EXE <x
   190  	run 'gccgo -O2 k-nucleotide-parallel.go' a.$EXE <x
   191  	run 'gc k-nucleotide' a.$EXE <x
   192  	run 'gc k-nucleotide-parallel' a.$EXE <x
   193  	run 'gc_B k-nucleotide' a.$EXE <x
   194  	rm x
   195  }
   196  
   197  mandelbrot() {
   198  	runonly echo 'mandelbrot 16000'
   199  	run "gcc $gccm -O2 mandelbrot.c" a.$EXE 16000
   200  	run 'gccgo -O2 mandelbrot.go' a.$EXE -n 16000
   201  	run 'gc mandelbrot' a.$EXE -n 16000
   202  	run 'gc_B mandelbrot' a.$EXE -n 16000
   203  }
   204  
   205  meteor() {
   206  	runonly echo 'meteor 2098'
   207  	run "gcc $gccm -O2 meteor-contest.c" a.$EXE 2098
   208  	run 'gccgo -O2 meteor-contest.go' a.$EXE -n 2098
   209  	run 'gc meteor-contest' a.$EXE -n 2098
   210  	run 'gc_B  meteor-contest' a.$EXE -n 2098
   211  }
   212  
   213  pidigits() {
   214  	runonly echo 'pidigits 10000'
   215  	if  $havegmp; then
   216  		run "gcc $gccm -O2 pidigits.c -lgmp" a.$EXE 10000
   217  	fi
   218  	run 'gccgo -O2 pidigits.go' a.$EXE -n 10000
   219  	run 'gc pidigits' a.$EXE -n 10000
   220  	run 'gc_B  pidigits' a.$EXE -n 10000
   221  }
   222  
   223  threadring() {
   224  	runonly echo 'threadring 50000000'
   225  	run "gcc $gccm -O2 threadring.c -lpthread" a.$EXE 50000000
   226  	run 'gccgo -O2 threadring.go' a.$EXE -n 50000000
   227  	run 'gc threadring' a.$EXE -n 50000000
   228  }
   229  
   230  chameneos() {
   231  	runonly echo 'chameneos 6000000'
   232  	run "gcc $gccm -O2 chameneosredux.c -lpthread" a.$EXE 6000000
   233  	run 'gccgo -O2 chameneosredux.go' a.$EXE 6000000
   234  	run 'gc chameneosredux' a.$EXE 6000000
   235  }
   236  
   237  case $# in
   238  0)
   239  	run="fasta revcomp nbody binarytree fannkuch regexdna spectralnorm knucleotide mandelbrot meteor pidigits threadring chameneos"
   240  	;;
   241  *)
   242  	run=$*
   243  esac
   244  
   245  for i in $run
   246  do
   247  	$i
   248  	runonly echo
   249  done
   250  
   251  rm *.o *.$EXE # Clean up
   252