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:
- when system under normal load (core usage 5-10%)
- 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:
- if using time command, output should consider? real, user or sys?
- is there standard method calculate running time of program?
- every time execute these commands, different reading. how many times should sample average same, given code not change.
- 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:
- depends on code doing each component of output of
timemay 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. - 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 usegettimeofdaytime not monotonically increase. system time can changed administrator or ntp process. should useclock_gettimeinstead. - to minimise runtime differences run run, check cpu frequency scaling off if you're getting wildly differing results. has caught me out before.
- once start getting multiple threads, might want start looking @ profiler. gprof place start.
Comments
Post a Comment