Question 4 a) Explain, with an example, how the use of multithreading enables a program to run faster. Use pseudocode or actual code to illustrate your answer. b) The following is non-concurrent code for working out the cosine of a sequence of integers. Rewrite this in Python so that it is concurrent. Your program must return the results in the order of the input sequence. This code is available with this TCA as Q4.zip. import math values = [12,20,4,100,64,77,200,9,10] def mycosine (n) : woonwww. return math.cos(n) ww wwwwwwww def non concurrent cosines(): wwwww for val in values: wwwww res = mycosine (val) wwwww print('Cosine of %s is %s' % (val,res), '\n') non concurrent cosines () wwwwww c) Use Python's datetime function to time the execution of the concurrent code, and to print out the result. Add the lines of code that support this to your solution to Question 4 (b). 1 import math 2 3 values = [12,20,4,100,64,77,200,9,10] 4 5 def cosine(n): 6 67 7 V return math.cos(n) 8 ✓ def non_concurrent_cosines(): v 9 v 10 11 12 for val in values: res = cosine(val) print('Cosine of %s is %s' % (val, res), '\n') 13 non_concurrent_cosines() 14

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
Question 4
a) Explain, with an example, how the use of multithreading enables a program to run faster.
Use pseudocode or actual code to illustrate your answer.
b) The following is non-concurrent code for working out the cosine of a sequence of
integers. Rewrite this in Python so that it is concurrent. Your program must return the
results in the order of the input sequence. This code is available with this TCA as Q4.zip.
import math
values = [12,20,4,100,64,77,200,9,10]
def mycosine (n) :
woonwww.
return math.cos(n)
ww
wwwwwwww
def non concurrent cosines():
wwwww
for val in values:
wwwww
res = mycosine (val)
wwwww
print('Cosine of %s is %s' % (val,res), '\n')
non concurrent cosines ()
wwwwww
c) Use Python's datetime function to time the execution of the concurrent code, and to print
out the result. Add the lines of code that support this to your solution to Question 4 (b).
Transcribed Image Text:Question 4 a) Explain, with an example, how the use of multithreading enables a program to run faster. Use pseudocode or actual code to illustrate your answer. b) The following is non-concurrent code for working out the cosine of a sequence of integers. Rewrite this in Python so that it is concurrent. Your program must return the results in the order of the input sequence. This code is available with this TCA as Q4.zip. import math values = [12,20,4,100,64,77,200,9,10] def mycosine (n) : woonwww. return math.cos(n) ww wwwwwwww def non concurrent cosines(): wwwww for val in values: wwwww res = mycosine (val) wwwww print('Cosine of %s is %s' % (val,res), '\n') non concurrent cosines () wwwwww c) Use Python's datetime function to time the execution of the concurrent code, and to print out the result. Add the lines of code that support this to your solution to Question 4 (b).
1 import math
2
3 values = [12,20,4,100,64,77,200,9,10]
4
5 def cosine(n):
6
67
7
V
return math.cos(n)
8 ✓ def non_concurrent_cosines():
v
9 v
10
11
12
for val in values:
res = cosine(val)
print('Cosine of %s is %s' % (val, res), '\n')
13 non_concurrent_cosines()
14
Transcribed Image Text:1 import math 2 3 values = [12,20,4,100,64,77,200,9,10] 4 5 def cosine(n): 6 67 7 V return math.cos(n) 8 ✓ def non_concurrent_cosines(): v 9 v 10 11 12 for val in values: res = cosine(val) print('Cosine of %s is %s' % (val, res), '\n') 13 non_concurrent_cosines() 14
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education