This document discusses techniques for improving programming and problem solving using Python. It covers topics like memory management, time optimizations, profiling, and common algorithms. For memory management, it discusses list vs iterator forms, the itertools module, and memoization. For time optimizations, it emphasizes choosing the right data structures and approaches.
This document discusses techniques for improving programming and problem solving using Python. It covers topics like memory management, time optimizations, profiling, and common algorithms. For memory management, it discusses list vs iterator forms, the itertools module, and memoization. For time optimizations, it emphasizes choosing the right data structures and approaches.
This document discusses techniques for improving programming and problem solving using Python. It covers topics like memory management, time optimizations, profiling, and common algorithms. For memory management, it discusses list vs iterator forms, the itertools module, and memoization. For time optimizations, it emphasizes choosing the right data structures and approaches.
(of course) yLhon uhruv 8aldawa [dhruvbaldawa AbouL Me 8.L. CompuLer Sclence from unlverslLy of Mumbal CSoCer 2011 and 2012 nauonal Cyber Clymplad Cold medalllsL 2007 CurrenLly worklng aL LnLhoughL, lnc. [dhruvbaldawa Why ls solvlng dlmculL ? Lhe daLaseL ls Loo large Lo be lLeraLe even once Lhe complexlLy ls huge or very bad or 8C1P !! [dhruvbaldawa WhaL Lhls Lalk covers Memory ManagemenL Common problems LlsL forms v/s lLeraLor forms lLerLools 1lme ManagemenL Chooslng Lhe correcL daLa sLrucLures Approach rollng ups [dhruvbaldawa Memory ManagemenL !"# %& %' %()*+',-' . MosL of Lhe common compeuuons requlre you sLore and compuLe large amounLs of daLa. 8unnlng ouL of memory llmlL ls preuy common. 1here ls always" a way Lo lmprove memory consumpuon of your program. [dhruvbaldawa Common roblems MemoryLrror[1] when you run ouL of memory, buL Lhe slLuauon can sull be rescued (by deleung cerLaln ob[ecLs) CverowLrror[1] when Lhe resulL of an arlLhmeuc operauon ls Loo large Lo be represenLed [dhruvbaldawa LlsL forms v/s lLeraLor forms LlsL forms beuer Lo sLore and re-use resulLs of compuLauons use lf you wanL Lo perform llsL operauons, where you need Lo sLore enure llsLs consumes much more memory only prefer when a real llsL cannoL be avolded all Lhe elemenLs are generaLed/lnluallzed aL once [dhruvbaldawa LlsL forms v/s lLeraLor forms lLeraLor forms lazy and on-demand generauon of values very low memory consumpuon very useful when you only need Lo work wlLh Lhe value aL hand /*-&0 cannoL sLep backwards cannoL sklp or [ump forwards Lhey are scalable and memory-frlendly and used where real llsLs are noL requlred [dhruvbaldawa LlsL forms v/s lLeraLor forms range v/s xrange[2] llsL comprehenslons v/s generaLor expresslons funcuons v/s generaLors [dhruvbaldawa lmporL lLerLools lLerLools module provldes a seL of fasL, memory emclenL lLeraLors provldes fasL lmplemenLauons for common [obs llke producL, permuLauons, comblnauons lLerLools documenLauon [dhruvbaldawa lmporL lLerLools [dhruvbaldawa lmporL lLerLools [dhruvbaldawa 1lme Cpumlzauons chooslng Lhe rlghL daLa sLrucLure chooslng Lhe rlghL approach [dhruvbaldawa chooslng Lhe rlghL daLa sLrucLure !"#$ &'()'#**+', -(''. #/(01 12+ 3($+4 5(($ &'()'#**+', -(''. #/(01 $#1# ,1'0310'+, #6$ 12+7' '+8#9(6,27&,4:
--Llnus 1orvalds [dhruvbaldawa uaLa SLrucLures ln yLhon don'L re-lnvenL Lhe wheel. use Luple, llsL, dlcL, seLs, as Lhey are coded ln C and hence are lAS1 for membershlp LesLs use dlcL/seL [C(1)] lnsLead of llsLs/Luple [C(n)] use collecuons Cueue operauons llke pop(), lnserL() are beuer ln collecuons.deque [C(1)] Lhan llsLs [C(n)] use blsecL, heapq for sorLed llsLs [dhruvbaldawa lmporL collecuons[4] deque CounLer CrderedulcL defaulLdlcL [dhruvbaldawa Memolzauon[3] cachlng resulLs from prevlous procedure calls, and uslng lL dlrecLly lf a funcuon call reLurns Lhe same value when Lhe same seL of argumenLs are passed, Lhen lL can be memolzed !
def memoized_function(value): if value in cache: return cache[value] else: cache[value] = compute(value) return cache[value] [dhruvbaldawa CollaLz Con[ecLure[6] f(n) = n/2, lf n ls even, = 3n+1, lf n ls odd