Comparing strings

4 views
Skip to first unread message

The Beez

unread,
Apr 27, 2026, 9:21:04 AM (2 days ago) Apr 27
to 4tH-compiler
Hi 4tH-ers!

If you paid attention the last days, you might have noticed I added a few string comparison routines - although we already have COMPARE - and have had it for many, many seasons.

No - why is that? It's a topic that has become hot in comp.lang.forth for the last few days. And I wrote COMP to add a little to that discussion.

Now - COMP is FAST!  But it helps very little if you go beyond merely establishing equality. So you cannot use it to sort strings. Anton threw his own weapon into the mix which was STRCMP - which is a full replacement of COMPARE.

So I took their benchmarks and collected the data. And they're not bad at all:

Results
compare : B1 = 0m4,578s; B2 = 0m1,234s; B3 = 0m4,839s
comp    : B1 = 0m3,911s; B2 = 0m0,932s; B3 = 0m0,716s
strcmp  : B1 = 0m4,049s; B2 = 0m1,147s; B3 = 0m4,300s

COMP has the edge - but it's a small one in the first two tests. Because it drops out immediately when the the length differs - it gets a very good score in the last test.

But overall - there's not much difference. COMPARE is only 13% slower  compared to STRCMP - and that translates to half a second over 10 MILLION iterations. Which is not bad at all IMHO, given it was written a quarter of a century ago.

Code included - and soon on SVN.

Hans Bezemer


strctest.4th
Reply all
Reply to author
Forward
0 new messages