DEV Community

Arpit Mohan
Arpit Mohan

Posted on • Originally published at insnippets.com

Making Python programs fast & why Go’s tooling is an undervalued tech

TL;DR notes from articles I read today.

Making Python programs blazingly fast

  • Find what parts of your code are slowing down the program. A simple & lazy solution is to use a Unix time command. You can also use cProfile for detailed profiling.
  • Once bottlenecks are identified, time the slow function without measuring the rest of the code. 
  • The most obvious way of making it faster is to use built-in data types.
  • Caching or memoization with lru_cache will help improve functions that perform expensive I/O operations or some fairly slow recursive functions.
  • You can improve performance, by using seemingly unnecessary assignments like local variables.
  • You can also speed up your code just by wrapping the whole code in the main function and calling it once.
  • Avoid or limit using dot operators (.) as they trigger dictionary lookup using getattribute, which creates extra overhead in your code.
  • Operations on strings like modulus (%s) or .format() can get quite slow when running in a loop. Go for f-string instead which is the most readable, concise and fastest method.

Full post here, 5 mins read


Go’s tooling is an undervalued technology

  • As it has no external dependencies, you can build the latest version of Go using just the compiler in seconds and cross-compiling only needs setting a couple of environmental variables (GOOS and GOARCH).
  • Go uses decentralized package management, or rather, module management. There is no central module manager or module registration, no external repository to trust and no need for an internal one.
  • Since a module only needs to be hosted on a reachable network with valid HTTPS certification, with the network path becoming the name, there is no worry over duplicating popular module names.
  • Dependencies are cryptographically locked to versions. So, an upstream source cannot change a published module for those depending on it.
  • As each dependency is a single point of failure, Go checks with a module proxy before fetching the dependency. If you prefer, there is a GOINSECURE option for experimentation to avoid HTTPS certification.

Full post here, 6 mins read


Design principles for your HTTP APIs

  • Impose consistency so that similar endpoints behave in similar ways, even in edge scenarios, with consistent vocabulary, URL structure, request/response formats and error handling. This will help ensure that users don’t need to read extensive documentation and handling updates becomes easier for developers.
  • Achieve performance by avoiding early optimization, waiting until you have the right metric in place to optimize based on hard data - and collect that data from day one, with an APM tool.
  • Use metrics to inform evolution, so that you update and add features based on actual user usage of endpoints to avoid or minimize disruptions to existing implementations.
  • For complex APIs, avoid a 1:1 mapping between database tables and API resources. Build in usability by simplifying business transactions to require a single API call rather than multiple. If it isn’t possible, be as flexible as you can.
  • Adopt simplicity by building on top of universally accepted standards and tools. This will mean less overheads and less room for mistakes.


Full post here, 7 mins read


Get these notes directly in your inbox every weekday by signing up for my newsletter, in.snippets().

Top comments (0)