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
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
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).](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F5bd4c086-c49b-4218-afb2-682b6bdbe49e%2Ff7c3a3e2-5c66-440f-b6e9-116d008b9714%2Fu3ssz1h_processed.png&w=3840&q=75)
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](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F5bd4c086-c49b-4218-afb2-682b6bdbe49e%2Ff7c3a3e2-5c66-440f-b6e9-116d008b9714%2Fs9yphnz_processed.png&w=3840&q=75)
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

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps

Recommended textbooks for you

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)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

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)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON

Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education