changeset 33290:1077e4739169

assert.m: Preallocate string for pretty printing The string being used for pretty printing assertion failures was being grown sequentially. In certain cases such as the BISTs for `conv2`, printing known failures was taking several minutes, especially when compiled without compiler optimization flags. This patch uses preallocation, leading to some 7x to 14x speedup. See https://octave.discourse.group/t/conv2-test-time-rather-long/5446/9
author Arun Giridhar <arungiridhar@gmail.com>
date Tue, 02 Apr 2024 12:53:50 -0400
parents 1db14e08bee4
children 85f39533661d 2e840d58dba7
files scripts/testfun/assert.m
diffstat 1 files changed, 18 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/testfun/assert.m	Mon Apr 01 22:21:02 2024 -0400
+++ b/scripts/testfun/assert.m	Tue Apr 02 12:53:50 2024 -0400
@@ -785,15 +785,24 @@
 
   str = ["ASSERT errors for:  assert " argin "\n"];
   str = [str, "\n  Location  |  Observed  |  Expected  |  Reason\n"];
-  for i = 1:length (err.index)
-    leni = length (err.index{i});
-    leno = length (err.observed{i});
-    lene = length (err.expected{i});
-    str = [str, sprintf("%*s%*s %*s%*s %*s%*s   %s\n",
-                  6+fix(leni/2), err.index{i}   , max (6-fix(leni/2), 0), "",
-                  6+fix(leno/2), err.observed{i}, max (6-fix(leno/2), 0), "",
-                  6+fix(lene/2), err.expected{i}, max (6-fix(lene/2), 0), "",
-                  err.reason{i})];
+
+  pos = numel (str);
+  str(end + 1e6) = ' ';
+  for i = 1:numel (err.index)
+    leni = numel (err.index{i});
+    leno = numel (err.observed{i});
+    lene = numel (err.expected{i});
+    tmp = sprintf("%*s%*s %*s%*s %*s%*s   %s\n",
+            6+fix(leni/2), err.index{i}   , max (6-fix(leni/2), 0), "",
+            6+fix(leno/2), err.observed{i}, max (6-fix(leno/2), 0), "",
+            6+fix(lene/2), err.expected{i}, max (6-fix(lene/2), 0), "",
+            err.reason{i});
+    if (pos + numel (tmp) >= numel (str))
+      str(end + 1e6) = ' ';
+    endif
+    str((pos + 1):(pos + numel (tmp))) = tmp;
+    pos += numel (tmp);
   endfor
+  str = str(1:pos);
 
 endfunction