How Fast is Your Computer?

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 < j =" 0," k =" nElements" endtime =" new">

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:

  • bit
  • numerical data types: int, long, float, double
  • two's complement notation (for representing positive and negative integers)
  • multi-tasking
  • CPU frequency
  • CPU clock period

Questions
What is the lowest number that can be represented in 32 bits using twos complement notation? In 64 bits?
What is the highest number that can be represented in 32 bits using two complement notation? In 64 bits?
What is the clock period for a processor that runs at 2.4 GHz?

Bibliography
Wikipedia contributors, 2005. "Primitive types". Wikipedia, The Free Encyclopedia [accessed January 6, 2006] http://en.wikipedia.org/w/index.php?title=Primitive_type&oldid=29101932
Wikipedia contributors, 2005. "Floating point". Wikipedia, The Free Encyclopedia [accessed January 6, 2006] http://en.wikipedia.org/w/index.php?title=Floating_point&oldid=33961235
Wikipedia contributors, 2005. "Two's complement". Wikipedia, The Free Encyclopedia [accessed January 6, 2006] http://en.wikipedia.org/w/index.php?title=Two%27s_complement&oldid=32853980
Wikipedia contributors, 2005. "Benchmark (computing)". Wikipedia, The Free Encyclopedia [accessed January 6, 2006] http://en.wikipedia.org/w/index.php?title=Benchmark_%28computing%29&oldid=32439222

Materials and Equipment

  • computer with Java-enabled web browser and Internet access
  • calculator or spreadsheet program for analyzing results

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.
    Click on the output text area.
    Type +a to select all of the contents.
    Type +c to copy the contents to system clipboard.
    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?

Variations

  1. Try running the applet on different computers whose processor type and speed you know and compare the results. Keep conditions as similar as possible (e.g., minimize the number of additional programs running). How do the times compare vs. speed within the same processor type? How do the times compare across processor types?
  2. Do your results change if you have other programs running while you make measurements with the applet? Does it matter if they are actually doing something or not?
  3. Here's an idea for analyzing your data further: how many clock cycles does each operation take? If you know the "speed" of your processor (in MHz or GHz), you can calculate this by multiplying the time per operation (in seconds) by the processor frequency (in hertz).
  4. For a more advanced project, use JavaScript to write your own program to investigate execution time for a function call or writing data to the screen. For example: how long does it take to generate a random number using Math.rand()? How does this compare with calculating the sine of an angle using Math.sin()? How does this compare to simple arithmetic operations in JavaScript? Can you estimate how many arithmetic operations are performed to generate a random number? How long does it take to write a line of text to the screen? How does this compare with changing the background color?
  5. For an even more advanced project, use a C or C++ compiler to write a native timing program. Java is an interpreted programming language, so it is likely that your native compiled C program will run faster than the Java applet. Is this the case?


Related Posts by Categories



0 comments:

Engineering News

Central Board of Secondary Education

Architecture News

Management News

Medical News

Journalism News

ss_blog_claim=39d0fbd9150037431cf33bbbf3c7c7ce