Writing efficient code that handles large data and tight constraints
How to write code that won't timeout on large inputs.
Before writing code, ask: how big is the data?
| Data size | Approach | |-----------|----------| | < 1 MB | Load into memory, any approach works | | 1-100 MB | Load into memory, but use efficient algorithms | | 100 MB - 1 GB | Stream/mmap, avoid loading entirely into memory | | > 1 GB | Streaming only, chunk-based processing |
mmap(), Python: mmap.mmap()) — map file into memory, OS handles pagingfread() in C, open(f, 'rb').read(chunk) in Pythonfgets() when you need random accesswrite() for every bytefwrite() or sys.stdout.buffer.write() for binary outputmmap() for large file access-O2 or -O3 for compiler optimizationsmalloc()/free() in tight loops — pre-allocatememcpy() instead of byte-by-byte copyingnumpy for numerical work (100x faster than pure Python loops)collections.Counter, defaultdict — avoid manual countingstruct.unpack() for binary parsingsubprocess.run() > os.system()wc -cSearch for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer
Tags:performance, optimization, benchmark