To empirically estimate the big-Oh in small programs

In summary, a teacher is preparing to give a discrete mathematics course where students will use Python for empirical estimation. The teacher is unsure how to incorporate a "counter" in the students' recursive programs and is looking for a solution that is both mathematically and computationally feasible. Suggestions include counting the number of operations, adjusting for different execution times, and using libraries such as SymPy. The teacher also discusses the possibility of collaborating with a colleague who will handle the computing aspect of the course.
  • #1
nomadreid
Gold Member
1,668
203

Homework Statement


First, I am a teacher preparing to give a discrete mathematics course, in which I will not do any computing, but the students will (in Python). I want them to make empirical estimates for the growth of their (short) recursive programs by putting in some sort of "counter" in their programs, and then run it on several sets of data of varying sizes. (I will also ask them to analyze theoretically, but that is not my question here.) But since I am not a programmer, I am not sure how to do this.

Homework Equations


N/A

The Attempt at a Solution


Simply have the program count the number of operations seems to me to be incorrect, because different operations will take different amounts of time and/or memory space. I am also aware that different machines (not to mention different versions of Python) will give different answers, but they should be within a linear factor of one another. Reading off the time for the completion of the program is not going to work, because the programs will be short. Some websites classify some operations complexity, but not all of them, and besides, that brings us back to a theoretical approach. Anyway, I don't want to actually give them the Python program construction (my students can program; I can't), but I will be suggesting the mathematical steps to put in. (Therefore an answer in mathematical language rather than computer language would be appreciated. I think the first mathematician to have seen a computer program reading "n=n+1" probably had a heart attack.)
Thanks for any hints.
 
Physics news on Phys.org
  • #2
nomadreid said:
Reading off the time for the completion of the program is not going to work, because the programs will be short.
You can always make the input longer (or let the programs run multiple times).
Most of the time you have some set of loops with just one or two different operations executed many times. If they do not get called the same amount of times, you can count both separately. Or add them. If one is just a factor of 2-3 slower than the other, it does not matter much compared to a different complexity.
nomadreid said:
my students can program; I can't
I don't think it is a good idea to give students tasks you cannot solve.
 
  • Like
Likes nomadreid
  • #3
you can also query for millisecs and and do stats on the deltas :

Code:
import time

def do_something_here():
  print "hello world"

# init timer
oldticks=time.clock()

# loop with timing
for i in range(1000):

  do_something_here()

  newticks=time.clock()
  delta=(newticks-oldticks)
  oldticks=newticks

  print "loop: ",i,"  delta: ",delta, " secs"
 
  • Like
Likes nomadreid
  • #4
Printing can take some time on its own, so better do it outside the areas where time is evaluated.
 
  • Like
Likes nomadreid
  • #5
Thanks, mfb and jedishrfu. I guess combining the answers, having the students run the program lots of times, subtracting the time taken to record the result from each run, and then doing stats on the results, should do it.
I do agree that giving students stuff that the instructor cannot solve is not a great idea; however, in this course, the computing part is relegated to a colleague with whom I split the course (I just do the maths part). That is, I know that the assignments I give are mathematically possible, but I need to give assignments that I know are computable in Python, even though my colleague will help them through the details. Therefore I am informing myself via this magnificent forum.
 
  • #6
Since, you're doing python and math together you both might want to check out Pyzo at pyzo.org. Its a collection of python libs + an interactive display where the environment is setup and ready to go. They don't have any example code but searching the web should find examples of how to use the various libraries available namely numpy, scipy, matplotlib ...
 
  • Like
Likes nomadreid
  • #7
Thanks, jedishrfu. The site looks very useful. In fact, the SymPy fits my idea what a computer should do. I shall recommend this to my students and to my colleague.
 

1. What is the purpose of empirically estimating the big-Oh in small programs?

The purpose of empirically estimating the big-Oh in small programs is to gain a better understanding of the time complexity of the program. This can help in making improvements to the program and optimizing its performance.

2. How is big-Oh notation used in the process of estimating time complexity?

Big-Oh notation is used to represent the upper bound of the time complexity of a program. By determining the big-Oh of a program, we can understand how the program's execution time will increase as the input size grows.

3. What are some common methods for empirically estimating big-Oh in small programs?

Some common methods for empirically estimating big-Oh in small programs include using timing functions, profiling tools, and analyzing the code's execution time for different input sizes.

4. What are some potential challenges in empirically estimating big-Oh in small programs?

One potential challenge is ensuring that the results are accurate and representative of the program's time complexity. This can be affected by the hardware and environment on which the program is run, as well as the accuracy of the measurement tools used.

5. How can the results of empirically estimating big-Oh in small programs be used?

The results of empirically estimating big-Oh in small programs can be used to make informed decisions about optimizing the program and improving its efficiency. It can also help in comparing the time complexity of different algorithms and choosing the most efficient one for a given problem.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • STEM Academic Advising
Replies
12
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
29
Views
2K
Replies
5
Views
1K
Replies
17
Views
929
  • Programming and Computer Science
Replies
22
Views
906
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Programming and Computer Science
2
Replies
54
Views
3K
  • Programming and Computer Science
Replies
8
Views
866
Back
Top