I would like to measure the system time it takes to execute some code. To do this I know I would sandwich said code between two calls to getrusage(), but I get some unexpect
Use gprof. This will give give you the time taken by each function.
Install gprof and use these flags for compilation -pg -fprofile-arcs -ftest-coverage.