Reputation: 17040
I am solving a numerical approximation (square root of two) up to a million or more digits of precision. There is little efficiency improvement I can make because of the insufficient knowledge I guess. But I do have a few questions related to Python itself which may help reduce some overhead times ....
If we are doing a calculation that will last several hours,how much of a constant factor can I reduce by moving a one-time conditional statement outside the while loop (IT WILL ONLY BE USED ONCE ANYWAY)? Is it just n*constant factor C ?
Upon some readings, I was told that list.append(x) is costly. Inside the while loop, each iteration will append a new integer into the list (which I also have to str(x) first....). def method6():
return ''.join([num
for num in xrange(loop_count)])
Reference reading: Efficient String Concatenation in Python
I am interested in this method. Can I use this to build the list and also join each item in the list into one-single string later when I return from the function (i.e. I need to print out the result). I am not too sure how to use this method. what is the first num? (Please visit the link because stackoverflow removes the quotes of the first num)
Following up with #2, I am inserting integers into the list
print result
[1L, '.', 4L, 1L, 4L, 2L, 1L]
print result[0]
1
I don't remember what that letter L stands for.....
I appreciate any helps! Thank you very much.
Upvotes: 2
Views: 1116
Reputation: 527338
Appending to a list is not costly. Appending to a string is costly.
This is because strings in Python are immutable, so every time you append, a completely new string is made. Lists, however, are mutable, so appending to a list simply adds a single item at the end.
The backing implementations of lists in most Python implementations are arrays, so lists that grow over time will need to be re-allocated a few times if initialized as a smaller size, but since most implementations tend to use an exponential growth factor for allocation sizes, it's not all that much of a problem (and if you wanted, you could avoid it by pre-allocating the entire length via something like my_list = [None]*1000000
for a list of a million None
s).
Upvotes: 1
Reputation: 57544
(Please try to limit questions to one topic, rather than asking several unrelated questions at once.)
If you want to know how much of a difference a change like moving something out of a loop makes, you need to benchmark it yourself. Search for timeit
.
Appending to a string and to a list are completely different beasts.
Strings are immutable--you don't append to them. a += 'x'
doesn't append 'x' to a; it creates an entirely new string, making it O(n). This can be inefficient when appending a lot of strings, since it becomes O(n^2). (Note, though, that CPython optimizes this particular case when there are no other references to a
, eliminating the extra copy. Not all Python implementations can do this.)
This problem doesn't exist with lists: lists are mutable. Appending to a list is O(1), so appending to them in a loop in efficient. (It will periodically need to reallocate the list to make room for new items, but that's efficient.)
If you're generating a lot of data to insert into a list, it's often preferable to use a generator function, but you don't have to.
In Python 2, 1L
declares a long, rather than an int; see http://docs.python.org/library/stdtypes.html#numeric-types-int-float-long-complex.
Upvotes: 4