]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
tests/ftests: speed up is_output_same() line compare loop
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Sat, 17 Jan 2026 05:09:17 +0000 (10:39 +0530)
committerTom Hromatka <tom.hromatka@oracle.com>
Wed, 11 Mar 2026 14:18:31 +0000 (08:18 -0600)
is_output_same() used to call expected_out.splitlines() on every
iteration and index into the freshly built list. Switching to
enumerate(zip(out_lines, exp_lines)) lets us split both strings once
and walk the pairs directly.

A quick timeit run (1,000,000 iterations with matching output) shows
the new loop shaving ~5% off the runtime:

old: 0.18783240672200918
new: 0.1784688732586801

While tiny in absolute terms, this removes needless allocations and
makes the helper more future-proof.

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
tests/ftests/utils.py

index c52678635bb9ca2d306c4dbbab30bb971edbc811..c977fd913d2dcd2d2d1aeb404fe9be4890a00fb1 100644 (file)
@@ -93,13 +93,17 @@ def is_output_same(config, out, expected_out):
     result = True
     cause = None
 
-    for line_num, line in enumerate(out.splitlines()):
-        if line.strip() != expected_out.splitlines()[line_num].strip():
+    out_lines = out.splitlines()
+    exp_lines = expected_out.splitlines()
+
+    for line_num, (line, exp_line) in enumerate(zip(out_lines, exp_lines)):
+        if line.strip() != exp_line.strip():
             cause = (
                     'Expected line:\n\t{}\nbut received line:\n\t{}'
-                    ''.format(expected_out.splitlines()[line_num].strip(), line.strip())
+                    ''.format(exp_line.strip(), line.strip())
                     )
             result = False
+            break
 
     return result, cause