“Performance” or “response time.” defines how fast an application presents an output once given an input. Assume you are building an app, and one of the engineers on the team thinks the Sieve of Eratosthenes algorithm will be the bottleneck in satisfying performance requirements. They have executed the algorithm and set it under test to create a performance baseline. (Please, find below A4.py and primeSieve.py) Run A4.py and report the result from the part 1 section: “X function calls in Y seconds.” After the remainder of the application and main algorithms are written, the engineers set the full application under performance testing, in part 2 of A4.py. Record the result from the part 2 section: “X function calls in Y seconds.” The method described in this section is to examine performance by making an educated guess on where performance bottlenecks occur before finishing and testing the entire application (or at least a complete portion of a significant function) Was it a good idea? Why or why not? Compose a short performance requirement for the (full) application, Describe why it is essential to write “good” performance requirements and what establishes “good.” *************************************************** A4.Py from primeSieve import * print 'Welcome to Assignment 4\n'print 'Example of print primeSieve(100)\n'print primeSieve(100) print '\nPart 1 - lets do a timing of primeSieve:\n' def full_calculation(x):if isPrime(x):x=x+1return math.sqrt(x)+ math.sin(x)/(x+1) #Part 1 -- Believed to be the critical algorithm#review the cProfile manual page numberOfPrimes = 1000000import cProfile, pstats, StringIOpr = cProfile.Profile()pr.enable() primeSieve(numberOfPrimes) pr.disable()s = StringIO.StringIO()sortby = 'cumulative'ps = pstats.Stats(pr, stream=s).sort_stats(sortby)ps.print_stats()print s.getvalue() print '\nPart 2 - lets do a timing of the (dummy) full calculation:\n'#Part 2 -- The full applicationpr2 = cProfile.Profile()pr2.enable() primes=primeSieve(numberOfPrimes)map(full_calculation, primes) pr2.disable()s2 = StringIO.StringIO()sortby = 'cumulative'ps2 = pstats.Stats(pr2, stream=s2).sort_stats(sortby)ps2.print_stats()print s2.getvalue() *************************************************** primeSieve.py # Prime Number Sieve# http://inventwithpython.com/hacking (BSD Licensed) import syssys.dont_write_bytecode = True import math def isPrime(num):# Returns True if num is a prime number, otherwise False. # Note: Generally, isPrime() is slower than primeSieve(). # all numbers less than 2 are not primeif num < 2:return False # see if num is divisible by any number up to the square root of numfor i in range(2, int(math.sqrt(num)) + 1):if num % i == 0:return Falsereturn True def primeSieve(sieveSize):# Returns a list of prime numbers calculated using# the Sieve of Eratosthenes algorithm. sieve = [True] * sieveSizesieve[0] = False # zero and one are not prime numberssieve[1] = False # create the sievefor i in range(2, int(math.sqrt(sieveSize)) + 1):pointer = i * 2while pointer < sieveSize:sieve[pointer] = Falsepointer += i # compile the list of primesprimes = []for i in range(sieveSize):if sieve[i] == True:primes.append(i) return primes ***************************************************
“Performance” or “response time.” defines how fast an application presents an output once given an input. Assume you are building an app, and one of the engineers on the team thinks the Sieve of Eratosthenes
(Please, find below A4.py and primeSieve.py)
- Run A4.py and report the result from the part 1 section: “X function calls in Y seconds.” After the remainder of the application and main algorithms are written, the engineers set the full application under performance testing, in part 2 of A4.py.
- Record the result from the part 2 section: “X function calls in Y seconds.”
- The method described in this section is to examine performance by making an educated guess on where performance bottlenecks occur
- before finishing and testing the entire application (or at least a complete portion of a significant function)
- Was it a good idea? Why or why not?
- Compose a short performance requirement for the (full) application, Describe why it is essential to write “good” performance requirements and what establishes “good.”
***************************************************
A4.Py
from primeSieve import *
print 'Welcome to Assignment 4\n'
print 'Example of print primeSieve(100)\n'
print primeSieve(100)
print '\nPart 1 - lets do a timing of primeSieve:\n'
def full_calculation(x):
if isPrime(x):
x=x+1
return math.sqrt(x)+ math.sin(x)/(x+1)
#Part 1 -- Believed to be the critical algorithm
#review the cProfile manual page
numberOfPrimes = 1000000
import cProfile, pstats, StringIO
pr = cProfile.Profile()
pr.enable()
primeSieve(numberOfPrimes)
pr.disable()
s = StringIO.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print s.getvalue()
print '\nPart 2 - lets do a timing of the (dummy) full calculation:\n'
#Part 2 -- The full application
pr2 = cProfile.Profile()
pr2.enable()
primes=primeSieve(numberOfPrimes)
map(full_calculation, primes)
pr2.disable()
s2 = StringIO.StringIO()
sortby = 'cumulative'
ps2 = pstats.Stats(pr2, stream=s2).sort_stats(sortby)
ps2.print_stats()
print s2.getvalue()
***************************************************
primeSieve.py
# Prime Number Sieve
# http://inventwithpython.com/hacking (BSD Licensed)
import sys
sys.dont_write_bytecode = True
import math
def isPrime(num):
# Returns True if num is a prime number, otherwise False.
# Note: Generally, isPrime() is slower than primeSieve().
# all numbers less than 2 are not prime
if num < 2:
return False
# see if num is divisible by any number up to the square root of num
for i in range(2, int(math.sqrt(num)) + 1):
if num % i == 0:
return False
return True
def primeSieve(sieveSize):
# Returns a list of prime numbers calculated using
# the Sieve of Eratosthenes algorithm.
sieve = [True] * sieveSize
sieve[0] = False # zero and one are not prime numbers
sieve[1] = False
# create the sieve
for i in range(2, int(math.sqrt(sieveSize)) + 1):
pointer = i * 2
while pointer < sieveSize:
sieve[pointer] = False
pointer += i
# compile the list of primes
primes = []
for i in range(sieveSize):
if sieve[i] == True:
primes.append(i)
return primes
***************************************************
Trending now
This is a popular solution!
Step by step
Solved in 6 steps with 3 images