TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Incrementally improving the performance of a Python script

113 pointsby Ivoahover 6 years ago

6 comments

sifoobarover 6 years ago
This is just a reflection from recent work on interpreter internals, but I figured it could be relevant to someone walking the same paths.<p>Allocating registers for all local vars statically means scopes have different sizes, which in turn complicates slab allocation and&#x2F;or reuse. In return for being easier to reason about (except for the main scope issue) and saving space.<p>I opted for a fixed number of linearly assigned registers per scope in Snigl [0]; once the limit is reached, remaining variables are stored in a table. Which means I sort of get both, since additional scopes may be added using {} (it could make sense to add a scope: keyword to Python) if that becomes an issue.<p>It&#x27;s all compromises, all the way down.<p>[0] <a href="https:&#x2F;&#x2F;gitlab.com&#x2F;sifoo&#x2F;snigl" rel="nofollow">https:&#x2F;&#x2F;gitlab.com&#x2F;sifoo&#x2F;snigl</a>
kiddicoover 6 years ago
Interesting. I guess I&#x27;ve been accidentally optimizing when I put everything in a main() call after an &quot;if __name__...&quot;
评论 #18689293 未加载
jacobsimonover 6 years ago
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:<p>[A.b[i] for i in range(100)]<p>is a lot slower than:<p>B = A.b<p>[B[i] for i in range (100)]
评论 #18689228 未加载
评论 #18690153 未加载
评论 #18689425 未加载
评论 #18690099 未加载
monochromaticover 6 years ago
I don’t think I understand the problem. We have n distinct integers, and the array has already been partitioned. Why isn’t the answer just that there was exactly one pivot that could generate that partition?
评论 #18688537 未加载
评论 #18688429 未加载
评论 #18688622 未加载
indwellerover 6 years ago
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 &quot;5; 1,2,3,4,4&quot;. Can someone help?<p>1. <a href="https:&#x2F;&#x2F;imgur.com&#x2F;a&#x2F;u8O65AF" rel="nofollow">https:&#x2F;&#x2F;imgur.com&#x2F;a&#x2F;u8O65AF</a><p>2. <a href="https:&#x2F;&#x2F;imgur.com&#x2F;a&#x2F;uHniZof" rel="nofollow">https:&#x2F;&#x2F;imgur.com&#x2F;a&#x2F;uHniZof</a>
评论 #18689084 未加载
评论 #18689037 未加载
评论 #18689103 未加载
kevinventulloover 6 years ago
Slightly OT from the main takeaway, but I wonder if this could be sped up further by only doing a single pass, and maintaining a stack (implicitly sorted) of elements which are greater than everything seen so far, and popping them off when they&#x27;re greater than the current element.