Python Forum
Amortized analysis: Insertion in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Amortized analysis: Insertion in a list
#1
I am trying to calculate the time is takes to append one element in a list. According to amortized analysis, it should be constant regardless how long the list is.

So, here is a little piece of code I've written to test it out with the corresponding output,
def calTime(n): from time import time data = [] start = time() for r in range(n): data.append(None) end = time() return ((end - start)/n) for r in [10**x for x in range(15)]: print("Size ",r, " time it took ", calTime(r), "sec")
Output:
Input is being redirected from C:\Projects\2698\input.txt Size 1 time it took 0.0 sec Size 10 time it took 0.0 sec Size 100 time it took 0.0 sec Size 1000 time it took 0.0 sec Size 10000 time it took 1.0018348693847657e-07 sec Size 100000 time it took 1.099705696105957e-07 sec Size 1000000 time it took 8.603405952453613e-08 sec Size 10000000 time it took 8.960003852844239e-08 sec
I have two questions,
  1. Is this the correct way to test if amortized cost of insertion in a dynamic array is O(1) - I am assuming it is correct as average insertion time for all values or n are very close to zero.
  2. I understand that the average time it takes to insert an elements in a list when n is small being rounded to zero. How do I print that small value?
Reply
#2
It is not a rounding problem. It is a timer problem. Your timer mas a smallest minimum time it can measure. Periods less than that may appear to be zero time.
quazirfan likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Insertion sort algorithm courtesy of YouTuber Joe James Drone4four 3 3,571 Dec-07-2020, 02:11 PM
Last Post: perfringo
  insertion sort viku361 1 3,014 Apr-20-2020, 01:47 PM
Last Post: deanhystad
  Detecting USB Device Insertion on Windows 10 Atalanttore 0 4,298 Jan-17-2020, 02:46 PM
Last Post: Atalanttore
  Tree insertion and variable referencing hshivaraj 3 4,680 Dec-10-2017, 04:29 PM
Last Post: Windspar

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.