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>
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