(window.webpackJsonp=window.webpackJsonp||[]).push([[2791],{3199:function(t,e,n){"use strict";n.r(e);var s=n(31),a=Object(s.a)({},(function(){var t=this,e=t.$createElement,n=t._self._c||e;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"python-speed-of-program"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#python-speed-of-program"}},[t._v("#")]),t._v(" Python speed of program")]),t._v(" "),n("h2",{attrs:{id:"deque-operations"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#deque-operations"}},[t._v("#")]),t._v(" Deque operations")]),t._v(" "),n("p",[t._v("A deque is a double-ended queue.")]),t._v(" "),n("div",{staticClass:"language-py extra-class"},[n("pre",{pre:!0,attrs:{class:"language-py"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("class")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("Deque")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("def")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("__init__")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("self"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n self"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("items "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("def")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("isEmpty")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("self"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" self"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("items "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("==")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("def")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("addFront")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("self"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" item"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n self"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("items"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("append"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("item"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("def")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("addRear")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("self"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" item"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n self"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("items"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("insert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("item"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("def")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("removeFront")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("self"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" self"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("items"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("pop"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("def")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("removeRear")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("self"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" self"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("items"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("pop"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("def")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("size")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("self"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("len")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("self"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("items"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n")])])]),n("p",[n("strong",[t._v("Operations : Average Case (assumes parameters are randomly generated)")])]),t._v(" "),n("p",[t._v("Append : O(1)")]),t._v(" "),n("p",[t._v("Appendleft : O(1)")]),t._v(" "),n("p",[t._v("Copy : O(n)")]),t._v(" "),n("p",[t._v("Extend : O(k)")]),t._v(" "),n("p",[t._v("Extendleft : O(k)")]),t._v(" "),n("p",[t._v("Pop : O(1)")]),t._v(" "),n("p",[t._v("Popleft : O(1)")]),t._v(" "),n("p",[t._v("Remove : O(n)")]),t._v(" "),n("p",[t._v("Rotate : O(k)")]),t._v(" "),n("h2",{attrs:{id:"algorithmic-notations"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#algorithmic-notations"}},[t._v("#")]),t._v(" Algorithmic Notations...")]),t._v(" "),n("p",[t._v("There are certain principles that apply to optimization in any computer language, and Python is no exception.\n"),n("strong",[t._v("Don't optimize as you go")]),t._v(":\nWrite your program without regard to possible optimizations, concentrating instead on making sure that the code is clean, correct, and understandable. If it's too big or too slow when you've finished, then you can consider optimizing it.")]),t._v(" "),n("p",[n("strong",[t._v("Remember the 80/20 rule")]),t._v(":\nIn many fields you can get 80% of the result with 20% of the effort (also called the 90/10 rule - it depends on who you talk to). Whenever you're about to optimize code, use profiling to find out where that 80% of execution time is going, so you know where to concentrate your effort.")]),t._v(" "),n("p",[n("strong",[t._v('Always run "before" and "after" benchmarks')]),t._v(":\nHow else will you know that your optimizations actually made a difference? If your optimized code turns out to be only slightly faster or smaller than the original version, undo your changes and go back to the original, clear code.")]),t._v(" "),n("p",[t._v("Use the right algorithms and data structures:\nDon't use an O(n2) bubble sort algorithm to sort a thousand elements when there's an O(n log n) quicksort available. Similarly, don't store a thousand items in an array that requires an O(n) search when you could use an O(log n) binary tree, or an O(1) Python hash table.")]),t._v(" "),n("p",[t._v("For more visit the link below...\n"),n("a",{attrs:{href:"https://wiki.python.org/moin/PythonSpeed/PerformanceTips",target:"_blank",rel:"noopener noreferrer"}},[t._v("Python Speed Up"),n("OutboundLink")],1)]),t._v(" "),n("p",[t._v("The following 3 asymptotic notations are mostly used to represent time complexity of algorithms.")]),t._v(" "),n("li",[n("p",[t._v("**Θ Notation**:\nThe theta notation bounds a functions from above and below, so it defines exact asymptotic behavior.\nA simple way to get Theta notation of an expression is to drop low order terms and ignore leading constants. For example, consider the following expression.\n3n3 + 6n2 + 6000 = Θ(n3)\nDropping lower order terms is always fine because there will always be a n0 after which Θ(n3) has higher values than Θn2) irrespective of the constants involved.\nFor a given function g(n), we denote Θ(g(n)) is following set of functions.\nΘ(g(n)) = {f(n): there exist positive constants c1, c2 and n0 such\nthat 0 <= c1**g(n) <= f(n) <= c2**g(n) for all n >= n0}\nThe above definition means, if f(n) is theta of g(n), then the value f(n) is always between c1**g(n) and c2**g(n) for large values of n (n >= n0). The definition of theta also requires that f(n) must be non-negative for values of n greater than n0.")])]),t._v(" "),n("li",[n("p",[t._v("**Big O Notation**: The Big O notation defines an upper bound of an algorithm, it bounds a function only from above. For example, consider the case of Insertion Sort. It takes linear time in best case and quadratic time in worst case. We can safely say that the time complexity of Insertion sort is O(n^2). Note that O(n^2) also covers linear time.\nIf we use Θ notation to represent time complexity of Insertion sort, we have to use two statements for best and worst cases:")])]),t._v(" "),n("ol",[n("li",[t._v("The worst case time complexity of Insertion Sort is Θ(n^2).")]),t._v(" "),n("li",[t._v("The best case time complexity of Insertion Sort is Θ(n).")])]),t._v(" "),n("p",[t._v("The Big O notation is useful when we only have upper bound on time complexity of an algorithm. Many times we easily find an upper bound by simply looking at the algorithm.\nO(g(n)) = { f(n): there exist positive constants c and\nn0 such that 0 <= f(n) <= cg(n) for\nall n >= n0}")]),t._v(" "),n("li",[t._v("**Ω Notation**: Just as Big O notation provides an asymptotic upper bound on a function, Ω notation provides an asymptotic lower bound.\nΩ Notation< can be useful when we have lower bound on time complexity of an algorithm. As discussed in the previous post, the best case performance of an algorithm is generally not useful, the Omega notation is the least used notation among all three.\nFor a given function g(n), we denote by Ω(g(n)) the set of functions.\nΩ (g(n)) = {f(n): there exist positive constants c and\nn0 such that 0 <= cg(n) <= f(n) for\nall n >= n0}.\nLet us consider the same Insertion sort example here. The time complexity of Insertion Sort can be written as Ω(n), but it is not a very useful information about insertion sort, as we are generally interested in worst case and sometimes in average case.")]),t._v(" "),n("h2",{attrs:{id:"notation"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#notation"}},[t._v("#")]),t._v(" Notation")]),t._v(" "),n("p",[n("strong",[t._v("Basic Idea")])]),t._v(" "),n("p",[t._v("The notation used when describing the speed of your Python program is called Big-O notation. Let's say you have a function:")]),t._v(" "),n("div",{staticClass:"language-py extra-class"},[n("pre",{pre:!0,attrs:{class:"language-py"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("def")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("list_check")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("to_check"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" the_list"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" item "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" the_list"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" to_check "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("==")]),t._v(" item"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("True")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("False")]),t._v("\n\n")])])]),n("p",[t._v('This is a simple function to check if an item is in a list. To describe the complexity of this function, you will say O(n). This means "Order of n" as the O function is known as the Order function.')]),t._v(" "),n("p",[t._v("O(n) - generally n is the number of items in container")]),t._v(" "),n("p",[t._v("O(k) - generally k is the value of the parameter or the number of elements in the parameter")]),t._v(" "),n("h2",{attrs:{id:"list-operations"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#list-operations"}},[t._v("#")]),t._v(" List operations")]),t._v(" "),n("p",[n("strong",[t._v("Operations : Average Case (assumes parameters are randomly generated)")])]),t._v(" "),n("p",[t._v("Append : O(1)")]),t._v(" "),n("p",[t._v("Copy : O(n)")]),t._v(" "),n("p",[t._v("Del slice : O(n)")]),t._v(" "),n("p",[t._v("Delete item : O(n)")]),t._v(" "),n("p",[t._v("Insert : O(n)")]),t._v(" "),n("p",[t._v("Get item : O(1)")]),t._v(" "),n("p",[t._v("Set item : O(1)")]),t._v(" "),n("p",[t._v("Iteration : O(n)")]),t._v(" "),n("p",[t._v("Get slice : O(k)")]),t._v(" "),n("p",[t._v("Set slice : O(n + k)")]),t._v(" "),n("p",[t._v("Extend : O(k)")]),t._v(" "),n("p",[t._v("Sort : O(n log n)")]),t._v(" "),n("p",[t._v("Multiply : O(nk)")]),t._v(" "),n("p",[t._v("x in s : O(n)")]),t._v(" "),n("p",[t._v("min(s), max(s) 😮(n)")]),t._v(" "),n("p",[t._v("Get length : O(1)")]),t._v(" "),n("h2",{attrs:{id:"set-operations"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#set-operations"}},[t._v("#")]),t._v(" Set operations")]),t._v(" "),n("p",[n("strong",[t._v("Operation : Average Case (assumes parameters generated randomly) : Worst case")])]),t._v(" "),n("p",[t._v("x in s : O(1)")]),t._v(" "),n("p",[t._v("Difference s - t : O(len(s))")]),t._v(" "),n("p",[t._v("Intersection s&t : O(min(len(s), len(t))) : O(len(s) * len(t)")]),t._v(" "),n("p",[t._v("Multiple intersection s1&s2&s3&...&sn : : (n-1) * O(l) where l is max(len(s1),...,len(sn))")]),t._v(" "),n("p",[t._v("s.difference_update(t) : O(len(t)) : O(len(t) * len(s))")]),t._v(" "),n("p",[t._v("s.symetric_difference_update(t) : O(len(t))")]),t._v(" "),n("p",[t._v("Symetric difference s^t : O(len(s)) : O(len(s) * len(t))")]),t._v(" "),n("p",[t._v("Union s|t : O(len(s) + len(t))")])])}),[],!1,null,null,null);e.default=a.exports}}]);