0

I'm working on a project for which I'm analyzing how quickly Newton's method can calculate a zero of a function from different starting points. I've written the program below and everything seems to be working except the calculation of the time difference. Because the method is quite fast, the time difference is also quite small and I think that because of that, Python interprets it as zero, so the output is nearly always zero. Is there any way to calculate this more accurately?

P.S.: I'm quite new to coding, so I'm sorry if my code is a bit of a mess, but I hope you can help me.

My code:

import math
import time
#enter function
def f(x):
 return x**2 - 4
#enter derivative
def fderiv(x):
 return 2*x
#Newton's method
def Newton(x):
 return (x - f(x)/fderiv(x))
#enter zero
zero = 2
#change starting point
for n in range(-100,100):
 start = zero + n/100
 #apply Newton on starting point
 current = start
 starttime = time.time()
 difference = abs(current - zero)
 try:
 while difference > 0.00001:
 current = Newton(current)
 difference = abs(current - zero)
 elapsed_time = time.time()-starttime
 print(str(start), ": elapsed time is: %.10f" % (elapsed_time))
 except:
 print(str(start), ": Error")

I've tried using some different methods, like datetime.fromtimestamp(), time.time_ns() or process_time(), but none of them seem to be working. The output is still mostly zeroes. Any ideas on how to fix it?

asked Feb 3, 2025 at 16:15
4
  • The standard answer would likely be stackoverflow.com/a/38319607/218663 but you also might want to check out the timeit mondule Commented Feb 3, 2025 at 16:39
  • Analysis of numerical algorithms is typically phrased in terms of numbers of iterations and numbers of arithmetic operations and function calls per iteration -- these are quantities which can be determined by studying the program and counting iterations; this approach factors out the dependence on the speed and other characteristics of the cpu. Hope this helps. Commented Feb 3, 2025 at 17:14
  • This question is similar to: How can I get millisecond and microsecond-resolution timestamps in Python?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Feb 4, 2025 at 0:55
  • This is an XY problem. You'll get some pretty serious diminishing returns from trying to increase your timer's precision. Instead, do a large number of iterations so the precision doesn't matter as much. timeit is a good choice for this, as JonSG suggested. Commented Feb 4, 2025 at 0:56

1 Answer 1

0

For high-resolution timing, use time.perf_counter().

answered Feb 3, 2025 at 23:52
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.