I need to time the amount of time it takes to run a recursive function vs. an iterative function. Everytime I run the program I get 0. Is there a way to calculate in miliseconds vs. just seconds? Here is some of my code so you know what I'm doing
1) Never include output in your measuring function. It is really slow. By outputting values you calculate you can slow down your calculation up to 100 times. It is unpredictable either, so any output you make will greatly increase measurement error.
2) Timer precision is low (on Windows CLOCKS_PER_SEC is usually 1000) and modern computers are really fast. Do at least 100 000 calculation (or more. you want at least 1 second of calculations) between measurements to make any sensible results.
Yes, and check perfomence for both large and small numbers (recursive will be way slower on high numbers). Then compare them. It will be something like:
Okay thank you! Also when I just do the iterative I get results like .02 and .35 but when running recursively, nothing even comes up. Does it really just take that much longer? Or am I doing something wrong? This is how I wrote the code
1 2 3
//run recursive function for small number
//call time
Sorry for all the questions, but thank you so much for your help!
Yes, naive recursive algorihm has complexivity of O(2^{n}). That is really large number. I suggest you to use (i%10 + 1) or slightly larger in your small numbers loop, and do not try to do large numbers loop. I forgot about terrible perfomance of naive recursive function.