|
|
|
|
|
by icebraining
4818 days ago
|
|
I think the similitude is deceptive. Since declarative programming is side-effect free, the runtime can perform the operation in the most efficient way it knows. Python isn't, so it has to ensure some execution order that breaks possible optimizations. In your example, if you replace max() with a function that prints something, you'll see it's executed for each value in 'table', which is extremely inefficient. This happens because Python can't guarantee that max() will return the same each time. Similarly, while in a side-effect free context the runtime could, for example, slice 'table', perform the work using multiple concurrent threads and then join the result, Python has to guarantee that the execution is done sequentially, since a change in order could affect the result. Unlike in SQL, in Python you're always telling it HOW you want it done. |
|
It's not.
> Since declarative programming is side-effect free, the runtime can perform the operation in the most efficient way it knows.
The point of declarative programming is to declare what you want done. The runtime may turn it into greatness or crap, that's not really relevant.
> In your example, if you replace max() with a function that prints something, you'll see it's executed for each value in 'table', which is extremely inefficient.
It's also not relevant and an implementation detail, with a known max() on a known type the implementation would be free to lift the computation out since this expression doesn't have side-effects. Just as a crappy SQL DB would be free to run the subquery once for each result.
> This happens because Python can't guarantee that max() will return the same each time.
The runtime can know that all types and functions involved are their native side-effect-less versions and is free to optimize them if it wishes, that it does not is irrelevant.
> Unlike in SQL, in Python you're always telling it HOW you want it done.
Nope, sorry, you're wrong.