Download presentation
Presentation is loading. Please wait.
1
March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class Young Scholars GrantsYoung Scholars Grants Exam Next TuesdayExam Next Tuesday Revised Lab 10: PrimesRevised Lab 10: Primes Calculating CPU Time UsedCalculating CPU Time Used Faster PrimesFaster Primes
2
March 2005 2R. Smith - University of St Thomas - Minnesota Exam Next Tuesday Chapter 2: ProgrammingChapter 2: Programming –Steps in creating a program Chapter 3: BasicsChapter 3: Basics –All of it: it’s pretty basic Chapter 4: VariablesChapter 4: Variables –Using a variable (“3 steps”) –Types int, char, and double –Arithmetic operators, incl % –Conversion and casting –Division and casting –‘printf’ codes for output Chapter 5: Loops –‘for’ loops –‘scanf’ for input –Relational operators –++ operation –Nested loops –‘while’ and ‘do’ loops Chapter 6: if Statement –‘else’ and ‘else if’ –Compound relationals –Nested ‘if’ Chapter 7: Arrays –1D arrays –Declaration, initialization –With a ‘for’ loop
3
March 2005 3R. Smith - University of St Thomas - Minnesota Revised Lab 10: Primes Group assignmentGroup assignment ALL MEMBER NAMES on source codeALL MEMBER NAMES on source code Read a number; generate that primeRead a number; generate that prime –Ex: 1st prime is 2, 6th prime is 13. Also, calculate the #primes per secondAlso, calculate the #primes per second –You must “time” your loop –Do a ‘double’ calculation for elapsed CPU seconds Do a good job: we’ll return to this laterDo a good job: we’ll return to this later
4
March 2005 4R. Smith - University of St Thomas - Minnesota Calculating Speeds The “time.h” headerThe “time.h” header The clock_t typeThe clock_t type The clock() functionThe clock() function ExampleExample
5
March 2005 5R. Smith - University of St Thomas - Minnesota What things take time? Each instruction takes about the same amount of timeEach instruction takes about the same amount of time What is an instruction?What is an instruction? –Assignments –Individual arithmetic operations –Individual logical comparisons –Each ‘if’ or loop is one instruction –‘for’ has at least 1 instruction per item in the heading Division might take longer than other thingsDivision might take longer than other things Faster = fewer instructions in the loopFaster = fewer instructions in the loop “Function calls” may be REALLY slow“Function calls” may be REALLY slow –‘printf’ is probably the slowest thing to do
6
March 2005 6R. Smith - University of St Thomas - Minnesota Improving prime calculations What are the two parts of finding primes?What are the two parts of finding primes? How can we improve the first step?How can we improve the first step? How can we improve the second step?How can we improve the second step?
7
March 2005 7R. Smith - University of St Thomas - Minnesota Improving prime calculations Two stepsTwo steps –Generating candidates –Testing candidates Improving the first stepImproving the first step –Loop that knows how to skip numbers –Odd numbers only –Are there other ways to skip? Improving the second stepImproving the second step –If trying all numbers, divide only by odd ones –Stop your search at the square root of the prime –Divide by primes only
8
March 2005 8R. Smith - University of St Thomas - Minnesota Thoughts? Ideas?
9
March 2005 9R. Smith - University of St Thomas - Minnesota Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by- sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.