2009年6月24日

KCachegrind

KCachegrind
Valgrind

// -- $ mkdir src; vim src/hello.c --
#include <stdio.h>
#include <stdlib.h>

static int function1(int x);
static int factorial(int n);

static void function2(int i) {
printf("%d\n", i);
if (i == 1) {
function1(i);
}
}

static int function1(int x) {
int i;
for (i = 0; i < x; i++) {
if (x != 1) {
function2(x);
} else {
return factorial(x);
}
}
return x + 1;
}

static int factorial(int n)
{
int ans;

if (n == 0)
{
ans = 1;
}
else
{
ans = n * factorial(n - 1);
}

return ans;
}

int main(int argc, char* argv[]) {
int i, total, count;

total = 0;

printf("5! = %d\n", factorial(5));
if (argc > 1) {
count = atoi(argv[1]);
} else {
count = 50;
}

for (i = 0; i < count; i++) {
total = function1(i);
function2(total);
}
return 0;
}

// -- $ vim makefile --
all: hello
valgrind --tool=callgrind --dump-instr=yes ./hello > /dev/null
kcachegrind

hello: src/hello.c
gcc -pg -g -o hello ./src/hello.c

clean:
rm -f gmon.out hello*
rm -f callgrind.out.*

// -- make --
$ make -i all
gcc -pg -o hello ./src/hello.c
valgrind --tool=callgrind ./hello > /dev/null
==22985== Callgrind, a call-graph generating cache profiler.
...

沒有留言:

網誌存檔