Anyway, there is no need to have 256 integers, just 2 is enough. When I try that, the results are similar: 17.5 ms (unsorted) / 12.5 ms (sorted)
968 karma · joined August 27, 2015
Anyway, there is no need to have 256 integers, just 2 is enough. When I try that, the results are similar: 17.5 ms (unsorted) / 12.5 ms (sorted)
I'm now thinking that the difference might be even larger if we instead avoid small integers and let the CPU get stuck chasing pointers. The idea is that it gets stuck on a memory access, which forces it to speculate much further, which in turn makes it backtrack a longer path if a branch was mispredicted. I'm obviously no expert on this, feel free to correct me
The results for 1B range instead of 255 are 17.6 ms for unsorted / 68.2 ms for sorted! We are back to what the original article observed and it's a way stronger effect than what branch prediction can offer. So don't sort your arrays, keep them in the order the boxed values were allocated ;)
It's the classic, why is processing sorted array faster than unsorted one
def f(arr):
r = True
for x in arr:
if x > 128: r = not r
return r
arr = [ random.randint(0, 255) for i in range(0, 1000_000) ]
arr_sorted = list(sorted(arr))
%timeit f(arr)
%timeit f(arr_sorted)
Results are (on my machine): 17.5 ms for unsorted, and 13.5 ms for sorted. For comparison, in a compiled language, the difference is 4xYes, but it might raise oil prices, effectively making the carbon tax apply globally. Unless OPEC just compensates by increasing production. IMHO carbon tax indeed better tool for EU/US, given that most of oil production out of "our" control
[1]: https://www.eccnet.eu/consumer-rights/what-are-my-consumer-r...
something(lambda x: [
x2 := x + 10, # define variables
[ print(f"Number: {i})
for i in range(x) ], # loop
[ 0 for i in range(x)
if print(f"Number: {i}") or True
if print(f"Second line: {i}") ], # loop if you prefer "normal code order"
0 if x > 0 else {}[f'{n} should be positive'], # conditions and asserts (throws KeyError, but includes the error message)
x2 * 2 # return
][-1])
note: I'm not responsible for coworkers injuring you after committing thisOn the other hand, requiring line terminators in the standard would inevitably lead to incompatibility issues. Most software would accept unterminated files, because text libraries do; and so some files will not be terminated. Some applications do not line-terminate files even on Linux (hello VSCode), and it would be even more problematic on Windows
...you can't compare minimalism/configurability in this way
* Works on all (most) distros * Builds the packages from sources, binaries are only a cache to speed things up. * Bonus: already has a lot of software packaged
And proprietary works in the system too, the build step can just download the binary from internet. It's not a preferred way to do it, but you don't have to build a broken system just because of few packages without source access
Many other algorithms are much harder, though, especially those requiring fast array indexing (graph search, hash tables, ...)
https://www.nushell.sh/book/commands/parse.html
I don't know how to do a similar thing in powershell.
> To me interesting question is that is it even possible to build rich strucure-aware shell-like cli environment that would allow seamless integration of external polyglot programs...
Problem is that most of the classic Unix programs output in some messy format. It would be nice if we could agree that each command has --json flag (for example) which will make the output trivial to parse programmatically in any other advanced shell.