4 ms·
[me@fedora ~]$ time perl -n -e 'length($_) == 10 && /^[^a]*a[^a]*$/i && /^[^b]*b[^b]*$/i && /^[^c]*c[^c]*$/i && /k.$/i && print' /usr/share/dict/words | wc -l
by oneshtein 3y ago
[me@fedora ~]$ time perl -n -e 'length($_) == 10 && /^[^a]*a[^a]*$/i && /^[^b]*b[^b]*$/i && /^[^c]*c[^c]*$/i && /k.$/i && print' /usr/share/dict/words | wc -l
26
real 0m0,168s
user 0m0,162s
sys 0m0,007s
[me@fedora ~]$ time perl -n -e '/^(?=.{8}k.$)(?=[^a]*a[^a]*$)(?=[^b]*b[^b]*$)(?=[^c]*c[^c]*$)/ && print' /usr/share/dict/words | wc -l
17
real 0m0,260s
user 0m0,254s
sys 0m0,006s
[me@fedora ~]$ time perl -n -e '/^.{8}k.$/ && /a/ && /b/ && /c/ && !/a.*a/ && !/b.*b/ && !/c.*c/ && print' /usr/share/dict/words | wc -l
17
real 0m0,115s
user 0m0,109s
sys 0m0,008s
[me@fedora ~]$ time bash -c "grep '^........k.\$' /usr/share/dict/words | grep a | grep b | grep c | grep -v 'a.*a' | grep -v 'b.*b' | grep -v 'c.*c'" | wc -l
17
real 0m0,015s
user 0m0,010s
sys 0m0,020s
[me@fedora ~]$ time awk '/^.{8}k.$/ && /a/ && /b/ && /c/ && !/a.*a/ && !/b.*b/ && ! /c.\*c/ { print }' /usr/share/dict/words | wc -l
17
real 0m0,129s
user 0m0,124s
sys 0m0,006s
[me@fedora ~]$ time perl -n -e 'local $_ = lc; length == 11 && substr($_,8,1) eq "k" && join("",sort [/([abc])/g]->@*) eq "abc" && print' /usr/share/dict/words | wc -l
17
real 0m0,234s
user 0m0,228s
sys 0m0,007s
- sltkr 3y agoYou can also do: sed -e '/^........k.$/!d; /a/!d; /b/!d; /c/!d; /a.*a/d; /b.*b/d; /c.*c/d' /usr/share/dict/words I wouldn't expect it to be much faster than AWK, but not much slower either. Interesting that the pipeline is the fastest! I would expect the sheer number of separate processes to slow things down considerably, but apparently it doesn't really matter. Probably because the first `grep` already filters out most of the dictionary, so there isn't a lot of I/O through the pipes.
- cptnapalm 3y agoThe pipeline wasn't just the fastest; it left everything else in the proverbial dust!
- oneshtein 3y ago[me@fedora ~]$ time sed -e '/^........k.$/!d; /a/!d; /b/!d; /c/!d; /a.*a/d; /b.*b/d; /c.*c/d' /usr/share/dict/words | wc -l 17 real 0m0,075s user 0m0,070s sys 0m0,006s
- hnlmorg 3y ago> Interesting that the pipeline is the fastest! I would expect the sheer number of separate processes to slow things down considerably, but apparently it doesn't really matter. Probably because the first `grep` already filters out most of the dictionary, so there isn't a lot of I/O through the pipes. The IO doesn’t matter so much because they’re done in parallel. Whereas the other examples have to filter each word throw the entirety of their steps before they can proceed onto the next word.
- oneshtein 3y agoBug fixed. [me@fedora ~]$ time perl -n -e 'length($_) == 11 && /^[^a]*a[^a]*$/i && /^[^b]*b[^b]*$/i && /^[^c]*c[^c]*$/i && /k.$/i && print' /usr/share/dict/words | wc -l 17 real 0m0,160s user 0m0,153s sys 0m0,008s