Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the time complexity of sum() in Python? [closed]

What is the time complexity of the sum() function?

like image 648
lzc Avatar asked Nov 22 '13 01:11

lzc


1 Answers

  1. It depends on your data structure. For a flat list, you cannot do better than O(n) because you have to look at each item in the list to add them up.

  2. When in doubt, try it out: import profile is your friend.

like image 66
Hugh Bothwell Avatar answered Oct 06 '22 13:10

Hugh Bothwell