In: Computer Science
Compare C++ speed to Python speed.
To compare Execution Time of: c++ vs. Py :
// this is c++ code // covert this code to python and compare their
execution time
#include<time.h>
#include <iostream>
using namespace std;
#include <chrono> //for time
using namespace std::chrono;
void doSomething() //to insert any execution code here to
measure its running time
{ int x=6795; // cout << "Enter 4-digit integer x ?\n"; cin
>> x;
for (int i = 1000; i < 2000000; i++) //a constant number of
iterations
{
if (i % 2) x = i + int(sqrt(x)); else x = i + x * x; //do any
computation here
}
}
void computeTime()
{ auto start = high_resolution_clock::now(); //starting time in
milli-seconds
doSomething();
auto stop = high_resolution_clock::now(); //stopping time in
milli-seconds
auto duration = duration_cast<microseconds>(stop - start);
//duration time in milli-seconds
cout << "duration= " << duration.count() <<
endl;
}
void main()
{
int more = 1;
while (more) { computeTime(); cout << "more? <0,1> ";
cin >> more; }
system("pause");
}
==================================================
// ( Python version. Be careful to put correct indentation for Python code below)
#---------- Now Python code to do the same as above code in
C++
import math
import datetime
def measureSpeed():
t1 = datetime.datetime.now().microsecond
x = 6795 # cout << "Enter 4-digit integer x ?\n"; cin
>> x;
#for (int i = 1000; i < 2000000; i++) //a constant number of
iterations
for i in range(1000,2000000,1):
if (i % 2):
x = i + math.sqrt(x) #x = i + int(sqrt(x));
else:
x = i + x * x #do any computation here
t2 = datetime.datetime.now().microsecond
print(t2)
print(t1)
print(t2-t1)
def main():
more=1
while(more):
more = input("Please enter <0 or 1> to stop or
continue:\n")
print(f'You entered {more}')
if(more):
measureSpeed()
main()
=====================================================
ANSWER:
CODE
#include<time.h>
#include <iostream>
#include <cmath>
using namespace std;
#include <chrono> //for time
using namespace std::chrono;
void doSomething() //to insert any execution code here to measure its running time
{
int x=6795; // cout << "Enter 4-digit integer x ?\n"; cin >> x;
for (int i = 1000; i < 2000000; i++) //a constant number of iterations
{
if (i % 2)
x = i + int(sqrt(x));
else
x = i + x * x; //do any computation here
}
}
void computeTime()
{
auto start = high_resolution_clock::now(); //starting time in milli-seconds
doSomething();
auto stop = high_resolution_clock::now(); //stopping time in milli-seconds
auto duration = duration_cast<microseconds>(stop - start); //duration time in milli-seconds
cout << "duration= " << duration.count() <<" Microseconds" << endl;
}
int main()
{
int more = 1;
while (more)
{
computeTime();
cout << "more? <0,1> ";
cin >> more;
}
system("pause");
}
import math
import datetime
def measureSpeed():
# starting time, using datetime.datetime.now()
t1 = datetime.datetime.now()
# required code
x = 6795 # cout << "Enter 4-digit integer x ?\n"; cin >> x;for (int i = 1000; i < 2000000; i++) //a constant number of iterations
for i in range(1000,2000000,1):
if (i % 2):
x = i + math.sqrt(x) #x = i + int(sqrt(x));
else:
x = i + x * x #do any computation here
# end time
t2 = datetime.datetime.now()
# calculating duration, in seconds using total_seconds method of timedelta
duration = (t2-t1).total_seconds()
# converting seconds to microseconds
durationMS = duration * (10**6)
# displaying results
print("duration = {:d} Microseconds".format(int(durationMS)))
# main function
def main():
more=1
# while loop, till more is 1
while(more):
# calling function measureSpeed
measureSpeed()
# prompting user, and converting input to integer using int function
more = int(input("Please enter <0 or 1> to stop or continue:\n"))
# calling main
main()
OUTPUT IMAGE