Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #101397
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Keith Thompson <kst-u@mib.org> |
| Newsgroups | comp.lang.c |
| Subject | Re: standard error codes |
| Date | Tue, 10 Jan 2017 12:12:02 -0800 |
| Organization | None to speak of |
| Lines | 67 |
| Message-ID | <ln7f62r7xp.fsf@kst-u.example.com> (permalink) |
| References | <96e16709-18eb-4399-86c4-a6f30a767df3@googlegroups.com> <212f08fd-b09e-4848-8485-f7480eaaae45@googlegroups.com> <bf564370-cac4-4030-8487-893c5ea71ddc@googlegroups.com> <86260b21-0fa2-42b4-93c6-08e9241d4c17@googlegroups.com> <lnk2a4qi1v.fsf@kst-u.example.com> <64011b84-2f8d-44f2-902a-0eacfcdf630b@googlegroups.com> <o510fi$16v$1@dont-email.me> <o51362$bse$1@dont-email.me> <11823fb6-613e-4283-95e8-dd930e152418@googlegroups.com> <o516ss$qp2$1@dont-email.me> <546841f1-b478-44dc-abc0-22ca4f46e553@googlegroups.com> <o51dsp$eb6$1@dont-email.me> <lnfukqrghw.fsf@kst-u.example.com> <o5387t$vp5$1@dont-email.me> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Injection-Info | mx02.eternal-september.org; posting-host="051d99dd7a883013689f38e075ee6474"; logging-data="22461"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Qq0ZJeWxTRxN9K2DDA22E" |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) |
| Cancel-Lock | sha1:4YxgntBLmTncazrEtPCaBq79qxM= sha1:ENqGvelOV+GyMmJbBnNmx4PiMOA= |
| Xref | csiph.com comp.lang.c:101397 |
Show key headers only | View raw
BartC <bc@freeuk.com> writes:
> On 10/01/2017 17:07, Keith Thompson wrote:
>> BartC <bc@freeuk.com> writes:
>> [...]
>>> (I just tested one of my intepreters counting the number of string
>>> equal/not equal ops, and the number of relative compare ops. The task
>>> was running a compiler. All ops were on counted strings. Results:
>>>
>>> string == != 140221 instances
>>> string < <= >= > 0 instances
>>>
>>> So memeq is needed!)
>>
>> memeq or streq, or both?
>
> These are counted strings so memeq. streq might be useful for strings
> for lengths are not known. If there was a choice of only one, then I
> think memeq would be more useful.
It would be odd to add memeq() but not streq() to the standard.
>> Either one would be needed only if replacing calls to memcmp/strcmp
>> by call to an optimized memeq/streq saves enough time to be
>> significant. If you can demonstrate that (memeq(x, y)) can be
>> significantly faster than (memcmp(x, y) == 0), then you'll have a
>> valid argument.
>
> If memcmp didn't exist and you wrote your own routine to compare two
> blocks of N bytes for equality, would you bother to also find out which
> of two unequal blocks was 'greater than' the other (once you've defined
> what that means), and set up a -1, 0 or 1 output code?
>
> I'm guessing you would take the simple approach!
But memcmp() does exist. Given that, the simple approach is to call
memcmp() and compare the result to 0 if all I care about is
(in)equality.
There's a sample implementation of strcmp() (but not memcmp() in K&R1.
It compares a byte at a time (reasonable in that context), and
distinguishing between less-than and greater-than (likely more useful
for strings than for byge buffers) is essentially free. It's the
difference between a subtraction and a comparison.
>> An experiment I did the other day shows that, at least for some
>> implementations, memcmp() returns the difference between the first
>> corresponding bytes that differ. Would returning a zero/nonzero
>> result be faster than returning the result of a subtraction?
>
> OK, so the result isn't even -1, 0 or 1!
Right. You could have found that out by reading the standard,
or a man page. You really should understand the existing standard
library functions before suggesting new ones.
> In that case it would be difficult to call, if getting a 1/0 result
> means an extra instruction instead of just returning the result of a
> subtraction.
No, it's not difficult to call. C programmers have been doing it for
decades. Is "> 0" harder to type than "== 1"?
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
standard error codes Thiago Adams <thiago.adams@gmail.com> - 2017-01-08 06:34 -0800
Re: standard error codes Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-01-08 11:05 -0800
Re: standard error codes Thiago Adams <thiago.adams@gmail.com> - 2017-01-09 03:33 -0800
Re: standard error codes Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-01-09 04:55 -0800
Re: standard error codes Keith Thompson <kst-u@mib.org> - 2017-01-09 09:06 -0800
Re: standard error codes Thiago Adams <thiago.adams@gmail.com> - 2017-01-09 09:52 -0800
Re: standard error codes BartC <bc@freeuk.com> - 2017-01-09 21:50 +0000
Re: standard error codes BartC <bc@freeuk.com> - 2017-01-09 22:36 +0000
Re: standard error codes supercat@casperkitty.com - 2017-01-09 14:51 -0800
Re: standard error codes BartC <bc@freeuk.com> - 2017-01-09 23:39 +0000
Re: standard error codes supercat@casperkitty.com - 2017-01-09 15:51 -0800
Re: standard error codes Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-01-09 16:08 -0800
Re: standard error codes supercat@casperkitty.com - 2017-01-09 18:51 -0800
Re: standard error codes Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-01-10 02:02 -0800
Re: standard error codes supercat@casperkitty.com - 2017-01-10 07:34 -0800
Re: standard error codes BartC <bc@freeuk.com> - 2017-01-10 01:39 +0000
Re: standard error codes Keith Thompson <kst-u@mib.org> - 2017-01-10 09:07 -0800
Re: standard error codes supercat@casperkitty.com - 2017-01-10 09:59 -0800
Re: standard error codes Keith Thompson <kst-u@mib.org> - 2017-01-10 12:02 -0800
Re: standard error codes Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-01-10 12:53 -0800
Re: standard error codes Keith Thompson <kst-u@mib.org> - 2017-01-10 13:03 -0800
Re: standard error codes Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-01-10 15:41 -0800
Re: standard error codes Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-11 02:56 +0000
Re: standard error codes supercat@casperkitty.com - 2017-01-11 07:42 -0800
Re: standard error codes supercat@casperkitty.com - 2017-01-10 12:59 -0800
Re: standard error codes BartC <bc@freeuk.com> - 2017-01-10 18:14 +0000
Re: standard error codes Keith Thompson <kst-u@mib.org> - 2017-01-10 12:12 -0800
Re: standard error codes BartC <bc@freeuk.com> - 2017-01-10 20:35 +0000
Re: standard error codes Keith Thompson <kst-u@mib.org> - 2017-01-10 13:00 -0800
Re: standard error codes supercat@casperkitty.com - 2017-01-11 08:16 -0800
Re: standard error codes Richard Heathfield <rjh@cpax.org.uk> - 2017-01-11 18:25 +0000
Re: standard error codes BartC <bc@freeuk.com> - 2017-01-11 19:09 +0000
Re: standard error codes Keith Thompson <kst-u@mib.org> - 2017-01-11 12:03 -0800
Re: standard error codes Richard Heathfield <rjh@cpax.org.uk> - 2017-01-11 20:10 +0000
Re: standard error codes Keith Thompson <kst-u@mib.org> - 2017-01-11 12:19 -0800
Re: standard error codes BartC <bc@freeuk.com> - 2017-01-11 20:31 +0000
Re: standard error codes Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2017-01-11 15:32 -0500
Re: standard error codes Richard Heathfield <rjh@cpax.org.uk> - 2017-01-11 20:48 +0000
Re: standard error codes Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2017-01-11 15:58 -0500
Re: standard error codes Keith Thompson <kst-u@mib.org> - 2017-01-11 13:01 -0800
Re: standard error codes Richard Heathfield <rjh@cpax.org.uk> - 2017-01-11 21:05 +0000
Re: standard error codes BartC <bc@freeuk.com> - 2017-01-11 21:08 +0000
Re: standard error codes Gareth Owen <gwowen@gmail.com> - 2017-01-11 21:38 +0000
Re: standard error codes BartC <bc@freeuk.com> - 2017-01-11 21:58 +0000
Re: standard error codes David Brown <david.brown@hesbynett.no> - 2017-01-12 12:11 +0100
Re: standard error codes Keith Thompson <kst-u@mib.org> - 2017-01-12 09:28 -0800
Re: standard error codes Richard Heathfield <rjh@cpax.org.uk> - 2017-01-12 18:06 +0000
Re: standard error codes Keith Thompson <kst-u@mib.org> - 2017-01-12 11:39 -0800
Re: standard error codes Richard Heathfield <rjh@cpax.org.uk> - 2017-01-12 19:50 +0000
Re: standard error codes Keith Thompson <kst-u@mib.org> - 2017-01-12 13:03 -0800
Re: standard error codes Richard Heathfield <rjh@cpax.org.uk> - 2017-01-12 21:13 +0000
Re: standard error codes <william@wilbur.25thandClement.com> - 2017-01-12 20:00 -0800
Re: standard error codes Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-01-13 01:56 -0800
Re: standard error codes Richard Heathfield <rjh@cpax.org.uk> - 2017-01-13 12:54 +0000
Re: standard error codes Keith Thompson <kst-u@mib.org> - 2017-01-13 09:35 -0800
Re: standard error codes Richard Heathfield <rjh@cpax.org.uk> - 2017-01-13 17:38 +0000
Re: standard error codes James Kuyper <jameskuyper@verizon.net> - 2017-01-13 13:06 -0500
Re: standard error codes raltbos@xs4all.nl (Richard Bos) - 2017-01-18 12:00 +0000
Re: standard error codes Richard Heathfield <rjh@cpax.org.uk> - 2017-01-18 12:13 +0000
Re: standard error codes Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-01-12 16:43 -0800
Re: standard error codes <william@wilbur.25thandClement.com> - 2017-01-12 19:34 -0800
Re: standard error codes Keith Thompson <kst-u@mib.org> - 2017-01-13 09:27 -0800
Re: standard error codes raltbos@xs4all.nl (Richard Bos) - 2017-01-12 19:33 +0000
Re: standard error codes Keith Thompson <kst-u@mib.org> - 2017-01-12 12:10 -0800
Re: standard error codes supercat@casperkitty.com - 2017-01-11 11:50 -0800
Re: standard error codes Richard Heathfield <rjh@cpax.org.uk> - 2017-01-11 20:38 +0000
Re: standard error codes jadill33@gmail.com - 2017-01-11 12:26 -0800
Re: standard error codes Richard Heathfield <rjh@cpax.org.uk> - 2017-01-11 20:44 +0000
Re: standard error codes Keith Thompson <kst-u@mib.org> - 2017-01-09 09:03 -0800
Re: standard error codes supercat@casperkitty.com - 2017-01-08 11:40 -0800
csiph-web