Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (anonymous):

http://ideone.com/QaVoJ this was one of my solutions to the hw1 in udacity robotics class. Why is it giving a veeeery slightly different answer to the question, compared to the standard solution?

OpenStudy (anonymous):

i can't understand anything in ur code where you use bazillion commands in one square brackets lol and why you use xrange?

OpenStudy (anonymous):

that's mine code http://ideone.com/SoZU3 but it's probably written in not pythonic way :D

OpenStudy (anonymous):

xrange is better than range if I'm just going to use the numbers; xrange is a generator that just yields numbers if you call it (requires less memory than an entire list), whereas range returns a list (requiring memory to store a whole list)

OpenStudy (anonymous):

what happens if i type a=xrange(5)?

OpenStudy (anonymous):

then a will be equal to a generator object 'xrange(5)'

OpenStudy (anonymous):

http://ideone.com/XhYt1

OpenStudy (anonymous):

so better always use xrange in for loop and use range only if you want to create list and assign it to some variable?

OpenStudy (anonymous):

right

OpenStudy (anonymous):

almost 2 times faster with 100millions >>> timeit.Timer('for i in xrange(100000000):pass').timeit(1) 2.7219030857086182 >>> timeit.Timer('for i in range(100000000):pass').timeit(1) 5.6206169128417969 but when i try to do with 1 billion >>> timeit.Timer('for i in xrange(1000000000):pass').timeit(1) 27.099134922027588 >>> timeit.Timer('for i in range(1000000000):pass').timeit(1) MemoryError

OpenStudy (anonymous):

lol... did the python list of 1 billion integers kill your system?

OpenStudy (anonymous):

not kill just memory error :D does it allow you to create such list?

OpenStudy (anonymous):

My system only has 1GB of memory :(

OpenStudy (anonymous):

why this doesn't work? a=[] timeit.Timer("for i in xrange(1000000000):a.append('')").timeit(1) gives NameError: global name 'a' is not defined timeit.Timer("a=[];for i in xrange(1000000000):a.append('')").timeit(1) gives SyntaxError: invalid syntax

OpenStudy (anonymous):

in a.append its double single '

OpenStudy (anonymous):

try defining a function, and then timing that. will it still give errors? never used timeit myself :(

OpenStudy (anonymous):

can you make void function? what my function should return? or just anything

OpenStudy (anonymous):

If you don't have a `return' keyword anywhere in the function, or if you use `return' alone (without an expression), then the function returns `None'

OpenStudy (anonymous):

NameError: global name 'testbillion' is not defined

OpenStudy (anonymous):

after 5minutes and 12 second got memory error, but my RAM usage didn't increase at all looking at system monitor

OpenStudy (anonymous):

however it created list of this length >>> len(a) 496880822

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!