Translation | Faster Python (Part 2)

Faster Python (Python Faster Way) uses code examples to demonstrate how writing Python code can lead to higher performance. This article explains the code, choosing the most suitable approach from the perspectives of performance and readability. Example 11: String Concatenation Worst/Best Time Ratio: 1.15 Recommendation: Use join when concatenating multiple (more than 3) strings at once; otherwise, use the plus sign or f-string. Explanation: This is another string concatenation issue, but the example is not well chosen. join is suitable for scenarios where multiple strings are concatenated at once, which is much faster than using the plus sign to concatenate multiple strings (the plus sign is equivalent to concatenating one by one). Example 12: Number Formatting ...

October 25, 2018 · 4 min · Zhiya

Stack Space Management in Go

Basic Logic of Stack Space Management The Go language provides support for concurrent programming through goroutines. Goroutines are a feature of the Go runtime library, not implemented as operating system threads, and can be understood as user-level threads. Since goroutines are managed by the Go runtime library, the library also needs to create and manage the corresponding stack space for each goroutine. The stack space allocated for each goroutine should not be too large, as it would waste a lot of space when many goroutines are created, nor too small, as it would lead to stack overflow. Go chooses a stack space management strategy where it starts with a relatively small space and automatically grows as needed. When a goroutine no longer needs such a large space, the stack space should also automatically shrink. ...

October 11, 2018 · 3 min · Zhiya

Translation | Faster Python (Part 1)

Faster Python (Python Faster Way) uses code examples to demonstrate how writing Python code can achieve higher performance. This article explains the code, selecting the most suitable approach from the perspectives of performance and readability. Example 1: String Formatting Worst/Best Time Ratio: 1.95 Recommendation: Use f-string for Python 3.7 or above, and the format method for other versions. Explanation: String formatting is a common scenario in code. Although using the + operator is optimal for concatenating a small number of strings, it results in the least readable code. If using Python 3.7 or above, f-string can solve this issue, offering better performance than both the format method and the % operator, while also being more readable than the + operator. Example 2: Dictionary Initialization ...

October 8, 2018 · 3 min · Zhiya

Pitfalls of 'defer' in Go

The defer statement is a very useful feature in Go, allowing a method to be delayed until the method enclosing it returns. In practical applications, defer can serve the role of try…catch… in other languages and can be used for handling cleanup operations such as closing file handles. Timing of defer Execution A “defer” statement invokes a function whose execution is deferred to the moment the surrounding function returns, either because the surrounding function executed a return statement, reached the end of its function body, or because the corresponding goroutine is panicking. ...

September 14, 2018 · 4 min · Zhiya

Writing Algorithms with Python | Implementing Reservoir Sampling for Random Sampling

Given a set of numbers where the total count is unknown, describe an algorithm that can randomly select k numbers from this set such that each number has an equal probability of being selected. If the set contains n numbers, then the probability of selecting each number is k/n. However, the challenge here is that the total count, n, is unknown. So how can we calculate the probability of selecting each number? ...

August 5, 2018 · 3 min · Zhiya