fix/add expected time complexity comments

This commit is contained in:
Oystein Kristoffer Tveit 2024-09-27 22:32:11 +02:00
parent 44fe044655
commit 3a6322f7c6
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
4 changed files with 12 additions and 3 deletions

View File

@ -1,6 +1,9 @@
lib: lib:
let let
# O(n * f)
genAttrsOld = names: f: lib.listToAttrs (map (n: lib.nameValuePair n (f n)) names); genAttrsOld = names: f: lib.listToAttrs (map (n: lib.nameValuePair n (f n)) names);
# O(n * f)
genAttrsNew = names: f: lib.listToAttrs (map (n: { name = n; value = f n; }) names); genAttrsNew = names: f: lib.listToAttrs (map (n: { name = n; value = f n; }) names);
bigdata = lib.imap0 (i: _: toString i) (lib.replicate 999999 null); bigdata = lib.imap0 (i: _: toString i) (lib.replicate 999999 null);

View File

@ -1,7 +1,12 @@
lib: lib:
let let
# O(n)? (assuming `name: attrs.${name}` is O(1))
getAttrsOld = names: attrs: lib.genAttrs names (name: attrs.${name}); getAttrsOld = names: attrs: lib.genAttrs names (name: attrs.${name});
# O(n^2)
getAttrsNew1 = names: attrs: lib.filterAttrs (n: _: builtins.elem n names) attrs; getAttrsNew1 = names: attrs: lib.filterAttrs (n: _: builtins.elem n names) attrs;
# O(n log m) (where m is the larger attrset, i.e. `attrs` in this case)
getAttrsNew2 = names: attrs: builtins.intersectAttrs (lib.genAttrs names (_: null)) attrs; getAttrsNew2 = names: attrs: builtins.intersectAttrs (lib.genAttrs names (_: null)) attrs;
bigdata = lib.genAttrs (lib.imap0 (i: _: toString i) (lib.replicate 999999 null)) (_: null); bigdata = lib.genAttrs (lib.imap0 (i: _: toString i) (lib.replicate 999999 null)) (_: null);

View File

@ -1,13 +1,13 @@
lib: lib:
let let
# O(nm) # O(n * m)
subtractListsOld = e: builtins.filter (x: !(builtins.elem x e)); subtractListsOld = e: builtins.filter (x: !(builtins.elem x e));
# O(n + m) (hopefully) # O(n + m) (hopefully)
subtractListsNew = e: let subtractListsNew = e: let
# Assuming genAttrs is O(n) # Assuming genAttrs is O(n * f) and f = (_: null) is O(1), so O(n) in total
e' = lib.genAttrs e (_: null); e' = lib.genAttrs e (_: null);
# Assuming hasAttr is O(1) # Assuming `filter` is O(n) and `hasAttr` is O(1)
in builtins.filter (x: !(builtins.hasAttr x e')); in builtins.filter (x: !(builtins.hasAttr x e'));
bigdata = lib.imap0 (i: _: toString i) (lib.replicate 999999 null); bigdata = lib.imap0 (i: _: toString i) (lib.replicate 999999 null);

View File

@ -1,5 +1,6 @@
lib: lib:
let let
# O(n^2), assuming foldl' is O(n) and ++ causes a reallocation every time
uniqueOld = builtins.foldl' (acc: e: if builtins.elem e acc then acc else acc ++ [ e ]) []; uniqueOld = builtins.foldl' (acc: e: if builtins.elem e acc then acc else acc ++ [ e ]) [];
# uniqueNew = xs: let # uniqueNew = xs: let
# entries = lib.genAttrs xs (_: null); # entries = lib.genAttrs xs (_: null);