c - Measure time complexity of a program in any programming language -


i searching standard way identify running time complexity of program. described here, not searching solution analyzing same looking @ code, rather through other parameters @ program runtime.

consider program requires user convert binary string decimal equivalent. time complexity such program should o(n) @ worst, when each binary digit processed @ time. intelligence, running time can reduced o(n/4) (process 4 digits binary string @ time, assume binary string has 4k digits k=1,2,3...)

i wrote program in c , used time command , function uses gettimeoftheday (both) calculate running time on linux box having 64 bit quad core processor (each core @ 800 mhz) under 2 categories:

  1. when system under normal load (core usage 5-10%)
  2. when system under heavy load (core usage 80-90%)

following readings o(n) algorithm, length of binary string 100000, under normal load:

time spent in user (ms) - 216 time spent in kernel (ms) - 8 timed using gettimeofday (ms) - 97 

following readings o(n) algorithm, length of binary string 200000, under high load:

time spent in user (ms) - 400 time spent in kernel (ms) - 48 timed using gettimeofday (ms) - 190 

what looking for:

  1. if using time command, output should consider? real, user or sys?
  2. is there standard method calculate running time of program?
  3. every time execute these commands, different reading. how many times should sample average same, given code not change.
  4. what if want use multiple threads , measure time in each thread calling execve on such programs.

from research have done, have not come across standard approach. also, whatever command / method seem use gives me different output each time (i understand because of context switches , cpu cycles). can assume here can solution machine dependant.

to answer questions:

  1. depends on code doing each component of output of time may significant. this question deals components mean. if code you're timing doesn't utilize system calls, calculating "user" time sufficient. i'd use "real" time.
  2. what's wrong time? if need better granularity (i.e. want time section of code instead of entire program) can start time before block of code profiling, run code , end time calculate difference give runtime. never use gettimeofday time not monotonically increase. system time can changed administrator or ntp process. should use clock_gettime instead.
  3. to minimise runtime differences run run, check cpu frequency scaling off if you're getting wildly differing results. has caught me out before.
  4. once start getting multiple threads, might want start looking @ profiler. gprof place start.

Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -