[A.b[i] for i in range(100)]
is a lot slower than:
B = A.b
[B[i] for i in range (100)]
11–20 of 24 posts
[A.b[i] for i in range(100)]
is a lot slower than:
B = A.b
[B[i] for i in range (100)]
Found something weird with codes posted in the links. I am getting different outputs for the intermediate and the final codes for the input of "5; 1,2,3,4,4". Can someone help? 1. https://imgur.com/a/u8O65AF 2. https://imgur.com/a/uHniZof
Found something weird with codes posted in the links. I am getting different outputs for the intermediate and the final codes for the input of "5; 1,2,3,4,4". Can someone help? 1. https://imgur.com/a/u8O65AF 2. https://imgur.com/a/uHniZof
> Starting from an array A that has n distinct integers I don't know in what ways they are differents, but these programs were not designed to work with duplicates in the input. This probably explains the results.
Python has some unusual performance behaviors. IIRC you can also speed up the performance of your program a lot by assigning intermediate variables instead of referencing properties, for example: [A.b[i] for i in range(100)] is a lot slower than: B = A.b [B[i] for i in range (100)]
Interesting. I guess I've been accidentally optimizing when I put everything in a main() call after an "if __name__..."
Python has some unusual performance behaviors. IIRC you can also speed up the performance of your program a lot by assigning intermediate variables instead of referencing properties, for example: [A.b[i] for i in range(100)] is a lot slower than: B = A.b [B[i] for i in range (100)]
For example, in CPython 3.6,
n = 0
d = 100
for i in range(10**6):
n += i
if n >= d:
n %= d
is slower than n = (n + i) % d
This counter to lower level languages, where dividing by a variable is costly, and the CPU can predict the pipeline to be false most of the time in the conditional and thus skip it.Python has some unusual performance behaviors. IIRC you can also speed up the performance of your program a lot by assigning intermediate variables instead of referencing properties, for example: [A.b[i] for i in range(100)] is a lot slower than: B = A.b [B[i] for i in range (100)]
Unusual is a strange qualifier here. Removing a dereference leading to a speedup is probably one of the few almost universal optimizations.
It is something that compilers do for you, in this context, in most programming environments.
Earlier quoted context omitted.
Unusual is a strange qualifier here. Removing a dereference leading to a speedup is probably one of the few almost universal optimizations.
People do it in C# a lot with Count and Length on Lists and Arrays and it ruins array bounds check elision. It is something that compilers do for you, in this context, in most programming environments.
(though perhaps a JIT could perform this optimization for the usual, non-surprising path)
Python has some unusual performance behaviors. IIRC you can also speed up the performance of your program a lot by assigning intermediate variables instead of referencing properties, for example: [A.b[i] for i in range(100)] is a lot slower than: B = A.b [B[i] for i in range (100)]
Unusual is a strange qualifier here. Removing a dereference leading to a speedup is probably one of the few almost universal optimizations.
Python has some unusual performance behaviors. IIRC you can also speed up the performance of your program a lot by assigning intermediate variables instead of referencing properties, for example: [A.b[i] for i in range(100)] is a lot slower than: B = A.b [B[i] for i in range (100)]