Related Links

  • Science Fair Project Guide

Project Summary

Difficulty  4 
Time required Very Short (a day or less)
Prerequisites None
Material Availability Requires computer with Java-enabled web browser (Java Runtime Envrionment version 1.5 or later) and Internet access
Cost Very Low (under $20)
Safety No issues

Donate to Science Buddies

Sponsor

Sponsored by a generous grant from Symantec Corporation

Internet Safety Tips
Get educated about online safety
with help from Symantec.

symantec.com/norton/familyresources

Objective

The goal of this project is to compare how long it takes to perform various mathematical operations with different variable types.

Introduction

Computer technology keeps advancing at an amazing pace. Today's home computers have more memory, run faster and are relatively less expensive than computers from ten or even five years ago. Is there a way to measure how fast your computer is? You might think that you could tell simply from the processor clock frequency, but this doesn't tell the whole story. The clock frequency just tells how many operations per second the CPU can perform. In modern multi-tasking operating systems, the CPU's time is split between many programs running at once. Also, the central processor (CPU) sends data to and receives data from other subsystems on the computer (e.g., memory, disk drives, video display) which usually run at slower speeds. Modern processors are equipped with high-speed data buffers (called caches) to alleviate these bottlenecks. There are also many strategies for optimizing the order of operations in a program for greatest efficiency.

In this project you will use a Java applet (see Experimental Procedure, below) to measure how long it takes for the computer to perform arithmetic operations (addition, subtraction, multiplication and division) with various data types (integers, long integers, floating point numbers and double-precision floating point numbers).

Measuring how long an operation takes provides useful information, both for optimizing algorithm performance, and also as a "benchmark" comparison between two computers. However, you must keep in mind that with today's multi-tasking operating systems, measuring the execution time of any single process is difficult. The operating system splits CPU time between all of the programs that are running. No program has "exclusive" access to the CPU. Generally, CPU processing speed is fast enough so that you don't notice this because programs appear to be instantly responsive. Behind the scenes though, each program is getting a slice of CPU time, then waiting for its next turn before it can run again.

So it is important to remember that, due to multi-tasking, the processing times you measure with the applet below will not represent the actual CPU time required to perform an addition or subtraction. In order for the applet to give you a best estimate, keep the number of open applications to a minimum, and make sure that any open applications are not performing tasks that require lots of CPU time (e.g., printing files or downloading content from the Internet).

How the Timing Applet Works

The applet works by repeating the arithmetic operations within two nested loops. In the inner loop, the program steps through an array (of the chosen data type: int, long, float or double) of 10,000 elements. The chosen operation (addition, subtraction, multiplication or division) is performed by pairing array elements: one counter (index "j", in the code snippet below) starts at the beginning of the array and counts up, the other (index "k" in the code snippet below) starts at the end of the array and counts down. Thus, the inner loop performs the chosen operation 10,000 times.

The outer loop (index "i" in the code snippet below) counts how many times the inner loop should be executed. The default choice is 10,000 times, which means that the arithmetic operation will be performed 10,0000 × 10,000 or 108 times.

The time it takes to execute these two nested loops is measured by the two calls to the Date().getTime() method, which returns the time with millisecond resolution. By subtracting startTime from endTime, the program determines how many milliseconds it took to execute the two nested loops. Again, it is important to remember that the operating system does not give the applet exclusive access to the CPU. The total time measured will also include some time that the operating system allocated to other programs while the applet was running. This is unavoidable, but do your best to minimize the number of other programs running.


   startTime = new Date().getTime();
   for(int i = 0; i < nIterations; i++) {
      for(int j = 0, k = nElements - 1; j < nElements; j++, k--) {
         iOut[j] = iIn[j] + iIn[k];
      }
   }
   endTime = new Date().getTime();
     Code snippet showing nested loops and getTime() method calls.

The applet is written in the Java programming language, which includes four different "primitive" data types for representing numbers: int, long, float and double. The "int" data type uses 32 bits to represent an integer, while the "long" data type uses 64 bits. So the "long" type can represent numbers with larger magnitude than the "int" type, but at the cost of taking up twice as much space, and perhaps taking longer time to process at the CPU. (You'll find out when you make your measurements.) Similarly, the "float" type is a 32-bit decimal number, while the "double" type is a 64-bit decimal number. Which data type do you predict will be processed most quickly? Which do you predict will be processed most slowly?

The two non-arithmetic operations, "no op" and "assign", can be used to calculate the loop "overhead" (the time the program spends incrementing and decrementing the loop variables, and assigning the result to the output array). In the "no op" case, there is an empty statement (literally no operation) in the inner loop. In the "assign" case, a constant value is copied to each member of the output array.

The controls of the applet are self-explanatory (including pop-up "tool tips" that display when the mouse hovers over a control). The "Run" button executes the currently chosen operation. Be careful not to set the number of iterations too high, or you'll be waiting a long time for it to finish. Rememeber that this is the count for the outer loop, and that the actual number of operations performed will be 10,000× this number (since there are 10,000 array elements to cycle through for each iteration of the outer loop).

The text box will show a report of all of the tests you've run. It will display the operation performed, the total number of operations (outer loop count × inner loop count), the total time this took (in milliseconds, abbreviation: ms) and the time per operation (in nanoseconds, abbreviation: ns). The time per operation is calculated by dividing the total time by the number of operations.

Terms, Concepts and Questions to Start Background Research

To do this project, you should do research that enables you to understand the following terms and concepts:

Questions

Bibliography

Materials and Equipment

Experimental Procedure

  1. First do your background research and make sure that you understand the terms and concepts and can answer the questions above.
  2. Use the applet below to measure the execution time for each of the operations for integers.
  3. Run the applet at least 10 times for each operation.
  4. You can use the keyboard to copy and paste the results from the applet to a spreadsheet program for analysis.
    1. Click on the output text area.
    2. Type <Ctrl>+a to select all of the contents.
    3. Type <Ctrl>+c to copy the contents to system clipboard.
    4. Finally, switch to your spreadsheet application and paste from the clipboard.
  5. As mentioned in the Introduction, the "no op" operation does nothing within the loop, so it provides a measure of the loop overhead. The "assign" operation simply copies a constant value to the output array (no calculation is performed). These two operations provide a control conditions. Both should take less time than any of the other operations. The time for the "int assign" operation can be subtracted from the time to complete the arithmetic operations for integers.
  6. Use a calculator or spreadsheet program to calculate the average time for each operation.
  7. Repeat your measurements and analysis (steps 1–6) for each of the four data types (int, long, float and double). You will want to measure the "assign" time separately for each data type.
  8. How do your predictions about processing time compare with the actual results? Were you surprised? If so, can you think of an explanation for what is going on? Can you think of another experiment you might try to test your explanation?

Note: if you see a gray box with a red "X" in the corner, you will need to update your Java Runtime Environment in order to run this applet. Go to http://www.java.com to get the latest version.

Variations

Credits

Andrew Olson, Ph.D., Science Buddies


Last edit date: 2006-02-22 00:25:30


Career Focus

science career image If you like this project, you might want to think about career opportunities in Computer Science.

Computers are essential tools in the modern world, handling everything from traffic control, car welding, movie animation, shipping, aircraft design, and social networking to book publishing, business management, music mixing, health care, agriculture, and online shopping. Computer programmers are the people who write the instructions that tell computers what to do. Learn more about this career: Computer Programmer.




Join Science Buddies

Become a Science Buddies member! It's free! As a member you will be the first to receive our new and innovative project ideas, news about upcoming science competitions, science fair tips, and information on other science related initiatives.


Support Science Buddies

If this website has helped you, won't you consider a small gift so we may continue developing resources to help teachers and students?

 



 

Science Buddies gratefully acknowledges its Presenting Sponsor
 
It's free! As a member you will be the first to receive our new and innovative project ideas, news about upcoming science competitions, science fair tips, and information on other science related initiatives.


Science Fair Project Home      Our Sponsors      Partners      About Us      Volunteer      Donate      Contact Us      Research Grants & Outreach      Site Map

Science Fair Project Ideas      Science Fair Project Guide      Ask an Expert      Blog      Teacher Resources      Parent Resources      Student Resources      Science Careers     


Privacy Policy Science Buddies

Copyright © 2002-2009 Kenneth Lafferty Hess Family Charitable Foundation. All rights reserved.
Reproduction of material from this website without written permission is strictly prohibited.
Use of this site constitutes acceptance of our Terms and Conditions of Fair Use.