Created
March 10, 2021 21:27
-
-
Save khanfarhan10/769cc66dee5a46d4acf7a028a55bc675 to your computer and use it in GitHub Desktop.
Evaluate the Running Time of the Scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ClassicalDifferential(x): | |
return differentialfx(x) | |
def NumericalDifferential(x): | |
return differentiate(f,x,h=1e-6) | |
def TorchDifferential(x): | |
x = torch.autograd.Variable(torch.Tensor([0.5]),requires_grad=True) | |
y = fnew(x) | |
y.backward() | |
return float(x.grad) | |
def RunDifferentiation(func): | |
DIFFS = [] | |
for i in range(1,100): | |
num = i/100 | |
diff = func(num) | |
DIFFS.append(diff) | |
def RunMultiple(func,n): | |
for i in range(n): | |
x= RunDifferentiation(func) | |
return 0 | |
import time | |
def TimeFuncRun(func, n=100): | |
start = time.time() | |
ans = RunMultiple(func,n) | |
return (time.time()-start)/n | |
n = 100 | |
print('Over ',n,' Iterations :') | |
print('ClassicalDifferential :', TimeFuncRun(ClassicalDifferential, n), 'seconds.') | |
print('NumericalDifferential :', TimeFuncRun(NumericalDifferential, n), 'seconds.') | |
print('TorchDifferential :', TimeFuncRun(TorchDifferential, n), 'seconds.') | |
n = 1000 | |
print('Over ',n,' Iterations :') | |
print('ClassicalDifferential :', TimeFuncRun(ClassicalDifferential, n), 'seconds.') | |
print('NumericalDifferential :', TimeFuncRun(NumericalDifferential, n), 'seconds.') | |
print('TorchDifferential :', TimeFuncRun(TorchDifferential, n), 'seconds.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment