summaryrefslogtreecommitdiff
path: root/perf.sh
blob: 2357d25edcf094f1ba4a1b2ba38a35bf2a7b9477 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#! /bin/sh

set -e

create_src() {
    n=$1
    i=0
    while [ $i -lt $n ]; do
        file=$i.cc
        cat <<EOF >$file
#include <algorithm>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
int var$i;
EOF
        i=$(($i + 1))
    done
}

compile() {
    n=$1
    compiler=$2
    i=0
    objdir=objs
    rm -rf $objdir
    mkdir -p $objdir
    while [ $i -lt $n ]; do
        echo "  $compiler -c -O2 $i.cc -o $objdir/$i.o"
        $compiler -c $i.cc -O2 -o $objdir/$i.o
        i=$(($i + 1))
    done
}

now() {
    perl -e 'use Time::HiRes qw(time); print time'
}

elapsed() {
    perl -e 'use Time::HiRes qw(time); printf("%.3f\n", time - $ARGV[0])' $1
}

###############################################################################

if [ -n "$CXX" ]; then
    cxx="$CXX"
else
    cxx=c++
fi

ccache=../ccache
tmpdir=tmpdir.$$
CCACHE_DIR=.ccache
export CCACHE_DIR
CCACHE_NODIRECT=1
export CCACHE_NODIRECT

rm -rf $tmpdir
mkdir $tmpdir
cd $tmpdir

n=30
create_src $n

echo "Without ccache:"
t0=$(now)
compile $n $cxx
echo "Time: $(elapsed $t0)"

echo "With ccache, no direct, cache miss:"
t0=$(now)
compile $n "$ccache $cxx"
echo "Time: $(elapsed $t0)"

echo "With ccache, no direct, cache hit:"
t0=$(now)
compile $n "$ccache $cxx"
echo "Time: $(elapsed $t0)"

unset CCACHE_NODIRECT
CCACHE_LOGFILE=ccache.log
export CCACHE_LOGFILE
rm -rf $CCACHE_DIR

echo "With ccache, direct, cache miss:"
t0=$(now)
compile $n "$ccache $cxx"
echo "Time: $(elapsed $t0)"

echo "With ccache, direct, cache hit:"
t0=$(now)
compile $n "$ccache $cxx"
echo "Time: $(elapsed $t0)"