- Functional Python Programming
- Steven F. Lott
- 286字
- 2021-08-27 19:20:20
Looking at object creation
In some cases, it might help to look at intermediate objects as a history of the computation. What's important is that the history of a computation is not fixed. When functions are commutative or associative, then changes to the order of evaluation might lead to different objects being created. This might have performance improvements with no changes to the correctness of the results.
Consider this expression:
>>> 1+2+3+4 10
We are looking at a variety of potential computation histories with the same result. Because the + operator is commutative and associative, there are a large number of candidate histories that lead to the same result.
Of the candidate sequences, there are two important alternatives, which are as follows:
>>> ((1+2)+3)+4 10 >>> 1+(2+(3+4)) 10
In the first case, we fold in values working from left to right. This is the way Python works implicitly. Intermediate objects 3 and 6 are created as part of this evaluation.
In the second case, we fold from right to left. In this case, intermediate objects 7 and 9 are created. In the case of simple integer arithmetic, the two results have identical performance; there's no optimization benefit.
When we work with something like the list append, we might see some optimization improvements when we change the association rules.
Here's a simple example:
>>> import timeit >>> timeit.timeit("((([]+[1])+[2])+[3])+[4]") 0.8846941249794327 >>> timeit.timeit("[]+([1]+([2]+([3]+[4])))") 1.0207440659869462
In this case, there's some benefit to working from left to right.
What's important for functional design is the idea that the + operator (or add() function) can be used in any order to produce the same results. The + operator has no hidden side effects that restrict the way this operator can be used.
- 程序員修煉之道:程序設計入門30講
- Learning Spring 5.0
- 微信公眾平臺開發:從零基礎到ThinkPHP5高性能框架實踐
- Linux命令行與shell腳本編程大全(第4版)
- 零基礎入門學習Python(第2版)
- Go語言精進之路:從新手到高手的編程思想、方法和技巧(1)
- Visual FoxPro程序設計習題集及實驗指導(第四版)
- Babylon.js Essentials
- Geospatial Development By Example with Python
- .NET 4.5 Parallel Extensions Cookbook
- Beginning C++ Game Programming
- Python計算機視覺和自然語言處理
- JavaScript編程精解(原書第2版)
- Mastering ASP.NET Core 2.0
- Java EE項目應用開發