I am doing a Introductory to Computer Science lab once a week. I was hoping to have a quick contest at the end of my next lab. I want to give them a block of code like this:
public class EfficientCode{
public static void main(){
long startTime, endTime, executionTime;
startTime = System.currentTimeMillis();
yourEfficientMethod():
endTime = System.currentTimeMillis();
executionTime = endTime – startTime;
}
public static void doSomething(){
// you do this part.
}
}
They will implement the doSomething method and the person with the fastest code will get a handful of bonus marks.
The problem is that the question needs to be somewhat simple. The students have a good grasp of: loops, if/else, Strings, adding, arrays, etc.
Here are my ideas for what the question could be:
I think in order for there to be a measurable difference in performance between methods you must do something many times.
Test effectiveness determines the effect of the test environment on the software/product. It is the number of test cases executed /unit of time. Time is generally in hrs. It is a number of bugs found/number of test cases executed. Q #1) How do you Test Code efficiency? Q #2) How do you measure Test Effectiveness and Efficiency?
Step 1: Convert the work into fraction i.e. per day work. Step 2: Express the fraction in the form of percentage by multiplying with 100. Let us see this with the help of an example. Example 1: If a person can complete his work in 5 days. What will be his efficiency? Answer: Number of days a person takes to complete his work = 5 days.
In the following section, we will see efficiency ratio and how we can solve all the problems on the concepts of time and work by the use of this technique. Let us see! If A is thrice as good a workman as B, then: The ratio of work done by A and B = 3: 1. The ratio of times taken by A and B to finish a work = 1 : 3.
The efficiency of work means, “How much work one person can do in one day (expressed in percentage)”. For example, a person can do a job in 2 days. In other words, we can say that he can do 50% of the work in one day. Therefore, his efficiency will be 50%.
Agreed about 'many times' for short operations, but for longer ones, once might be enough on its own.
I suggest looking into Project Euler, an excellent collection of programming questions. The best part is that the problems are designed with a "one minute rule" in mind, that most problems should take a moderate computer less than one minute to execute an efficient algorithm to find the answers. So an excellent place to start. :)
Two things.
First, efficiency is about more than execution time. It also is about memory usage, memory access, file-system/resource access, etc. There are tons of things that go into efficiency. So please be explicit that you're looking for the routine with the shortest run-time. Otherwise you are sending a mixed message...
Second, I heard this problem about 15 years ago, and I can't forget it:
Produce a list of all 5-digit number pairs that sum to 121212
. However, neither of the 2 numbers can repeat a decimal digit. So 1
can only appear once in either number. So an example result pair is 98167 + 23045
. There are a fair number, and it's easy to build a brute-force solution, but an efficient solution requires some thought. There are 192 unique pairs...
Because this is an introductory class and your students haven't covered sorting yet I think it's going to be very hard to come up with something simple enough to do, interesting enough to have a few different ways of doing it, and complex enough that there is an appreciable difference in speed between the different implementations on a modern computer. Your real problem, though, is that anything simple enough for them to try already has a canonical implementation only a short Google search away.
My suggestion is to invert the challenge. Have your students compete to come up with the gnarliest, slowest, most memory hogging solution they can think of. I believe it's as educationally valuable to think about all the wrong ways of doing something as it is to think about the right, and it's just as hard to be the worst as it is to be the best. It's easier to see results subjectively as well since bad code will be really slow. No Googling for an answer either. Finally, in my (irrelevant) opinion, this has the added bonus of making the challenge more fun.
Something like finding a string within another string is easier made bad than good. Maybe have them extract all the prime numbers from a 2kb string of random alphanumeric characters. Lots of ways to make a pig's ear of that problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With