5

For a project I am supposed to measure the running time of basic primitive operations in C++ for the machine that I am using. It says this:

Write a program that determines the values of the timing parameters of (fetch, store, +, -, *, /, <, function call, function return, new, delete, and []) for the machine on which it is run.

fetch and store is:

a = b + 1

b and 1 are being "fetched" (and added together with +), and stored in a.

I've never done something like this before. Doing I need to use a clock method to calculate the running time? Should the code that I am timing be complex or simple? How many times should it be evaluated?

template boy
  • 10,230
  • 8
  • 61
  • 97
  • If you're using an x86 machine it will have performance hooks. [See this answer](http://stackoverflow.com/questions/8091182/how-to-read-performance-counters-on-i5-i7-cpus). – Ross Rogers Sep 11 '14 at 20:06
  • @RossRogers Sorry I'm new to this. What's a performance hook? And how do I know I'm on an x86 machine? – template boy Sep 11 '14 at 20:43
  • When you're measuring the perf' of C++ operands very accurately you're actually going to need to have your CPU tell you how long they took. Intel's x86 chips have special performance monitors built into the chip. Anyways, if this is a homework project, then you may be able to go a less accurate route like doing the operand over and over and over in a tight loop and then timing the whole process. Alternatively, other people have measured the time of instructions and you could just tally add up the time of the operands based on the generated assembly code. – Ross Rogers Sep 11 '14 at 20:57
  • @RossRogers I'm have a AMD Turion 64 X2 CPU and I looked it up and wikipedia said it's for x86-64. I guess that's what I'm on. And this isn't a homework problem, I'm teaching myself. :) – template boy Sep 11 '14 at 21:02
  • Looks like you want AMD's performance counters then :-) Also, you may have to be in x86 ring-0 mode ( the OS privilege level), so you may have to do something like write a Linux kernel module. Anyways, this project could easily get out of hand :-) Remember ["Premature optimization is the root of all evil"](http://en.wikiquote.org/wiki/Donald_Knuth) The perf bottleneck is almost never what you think it is. You have to profile your code. – Ross Rogers Sep 11 '14 at 21:15

1 Answers1

1

I think this might be helpful. Personally I would make some loops with different number of iterations (starting from for example 10 and ending at 100 000). Similar to comparing sorting methods.

Ofc, if you need more complex method you can use performance hook mentioned before.

Community
  • 1
  • 1
Szczurson
  • 11
  • 3