473,471 Members | 2,533 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

opensolaris C library vs. GNU C Library

I have taken a look at the source code for a few string functions like
memcpy() and strstr() in OpenSolaris and GNU and I find them somewhat
different.

The GNU version of strstr() seems like a mess and very cluttered. Not sure
how fast it is but the OpenSolaris version of strstr() seems
straightforward.

Any reason why this is?

Also any comments, recommendation, stuff a newbie should know between the
two libraries,
OpenSolaris and GNU?
Open Solaris
http://cvs.opensolaris.org/source/xr.../util/string.c

Free Software Foundation (GNU)
http://wdiff.progiciels-bpi.ca/showf...t/lib/strstr.c
Jun 8 '06 #1
6 1892
smnoff wrote:
The GNU version of strstr() seems like a mess and very cluttered. Not sure
how fast it is but the OpenSolaris version of strstr() seems
straightforward.

Any reason why this is?
* I deliberately chose not to comment it. You should have at least
* as much fun trying to understand it, as I had to write it :-).

i think that pretty much says it all.
Also any comments, recommendation, stuff a newbie should know between the
two libraries,
OpenSolaris and GNU?


i'd recommend against adopting a similar philosophy when writing your
own code.

Jun 8 '06 #2
tedu a écrit :
smnoff wrote:
The GNU version of strstr() seems like a mess and very cluttered. Not sure
how fast it is but the OpenSolaris version of strstr() seems
straightforward.

Any reason why this is?

* I deliberately chose not to comment it. You should have at least
* as much fun trying to understand it, as I had to write it :-).

i think that pretty much says it all.

Yes. You are right. Most of the GNU code is like that.

Messy, and without a single comment, beides the license
stuff.

Open source means nothing at all since to understand
the stuff you have to spend more time than if you start
from scratch.

I wrote this:

extern int _stdcall GetTickCount(void);

int main(void)
{
char buf[65535];
char *p="Search";
int t,i;

memset(buf,'A',sizeof(buf)-1);
buf[sizeof(buf)-1] = 0;
strcpy(buf+sizeof(buf)-sizeof("Search")-1,p);
t = GetTickCount() ;
for (i=0; i<10000;i++) {
GNUstrstr(buf,"Search");
}
t = GetTickCount() - t;
printf("GNU=%d ms\n",t);
t = GetTickCount() ;
for (i=0; i<10000;i++) {
Sunstrstr(buf,"Seach");
}
t = GetTickCount() - t;
printf("Sun = %d ms\n",t);
}

The output is:
GNU=1313 ms
Sun = 1640 ms

If we change the strcpy line to more realistic test
conditions:
memcpy(buf+1000,p,6);

results change to:

GNU=16 ms
Sun = 31 ms

This means that both algorithms converge with large strings, what is not
surprising since input/output from/to main memory becomes the dominant
factor of the computation, and both algorithms are then the same.

With shorter strings GNU is twice as fast, but since the strings are
shorter the absolute difference is less (only 15ms after 10 thousand
runs!) so it is probably not worth the effort of understanding
the GNU version, full of goto's and a very complex control flow.

Surely I would not love being the maintainer of that stuff.

The size of the GNU function is 260 bytes, the Sun version is
only 110 bytes.

The GNU version would be important when strstr makes more than 90%
of the run time of your program. For all other situations Sun's version
is the same

jacob

Jun 8 '06 #3
jacob navia a écrit :

I wrote this:

extern int _stdcall GetTickCount(void);

int main(void)
{
char buf[65535];
char *p="Search";
int t,i;

memset(buf,'A',sizeof(buf)-1);
buf[sizeof(buf)-1] = 0; // Prepare a string of 64K having the searched
// for string at the end of the string, i.e.
// almost at the end of the 64K strcpy(buf+sizeof(buf)-sizeof("Search")-1,p);
t = GetTickCount() ;
for (i=0; i<10000;i++) {
GNUstrstr(buf,"Search");
}
t = GetTickCount() - t;
printf("GNU=%d ms\n",t);
t = GetTickCount() ;
for (i=0; i<10000;i++) {
Sunstrstr(buf,"Seach");
}
t = GetTickCount() - t;
printf("Sun = %d ms\n",t);
}

Excuse me. I was complaining about lack of comments in GNU stuff
and wrote a piece of code without any comment !!!!!

I hope is clearer now.

jacob
Jun 8 '06 #4
On Fri, 09 Jun 2006 00:16:34 +0200, jacob navia wrote:
The GNU version would be important when strstr makes more than 90% of the
run time of your program. For all other situations Sun's version is the
same


Except, with a poor library every time you re-implement and optimize one
function, another one will rise to the top. I'd rather a well honed
library where I didn't have to worry about such things and could focus on
my own code. So, if this difference were isolated you have a point, if not
shame on Sun.

Practically speaking, you want simple code and simple algorithms on the
edges of your software, where you spend the vast majority of your time
changing behavior.

Jun 9 '06 #5
On Fri, 09 Jun 2006 00:16:34 +0200, jacob navia
<ja***@jacob.remcomp.fr> wrote:
<snip: compare GNU vs Solaris ststr>
char buf[65535];
char *p="Search";
int t,i;

memset(buf,'A',sizeof(buf)-1);
buf[sizeof(buf)-1] = 0;
strcpy(buf+sizeof(buf)-sizeof("Search")-1,p);
If you made it char p [] = "Search" could use sizeof (p).
t = GetTickCount() ;
for (i=0; i<10000;i++) {
GNUstrstr(buf,"Search");
}
t = GetTickCount() - t;
printf("GNU=%d ms\n",t);
t = GetTickCount() ;
for (i=0; i<10000;i++) {
Sunstrstr(buf,"Seach");
}
t = GetTickCount() - t;
printf("Sun = %d ms\n",t);


I hope you actually searched for Search not Seach in the Solaris case
to be (completely) fair. Or to avoid such typos, use p in both cases.
- David.Thompson1 at worldnet.att.net
Jun 19 '06 #6
jacob navia wrote:
This means that both algorithms converge with large strings, what is not
surprising since input/output from/to main memory becomes the dominant
factor of the computation, and both algorithms are then the same.
This will probably not be true until you start swapping to disk.
Memory is still O(n), but is mitigated by bandwidth capabilities of
your hardware while the O(n) algorithm of the strstr() itself is still
dependent on the efficiency of the algorithm.
With shorter strings GNU is twice as fast, but since the strings are
shorter the absolute difference is less (only 15ms after 10 thousand
runs!) so it is probably not worth the effort of understanding
the GNU version, full of goto's and a very complex control flow.
Because you're going to get so much more understanding out of "you
can't shift a negative number on some platforms". It *IS* worth a
minute or two of study, because the author didn't comment it, yet it
achieves better performance. If you figure it out, you may be able to
achieve that performance without the non-maintainability penality. You
instead prefer to stay in the dark for lack of wanting to look at a 100
lines of code?
Surely I would not love being the maintainer of that stuff.
In the case of strstr(), or other well defined library functions, its
actually quite easy to maintain it, regardless of how convoluted it is.
Just write tests for it.
The size of the GNU function is 260 bytes, the Sun version is
only 110 bytes.

The GNU version would be important when strstr makes more than 90%
of the run time of your program. For all other situations Sun's version
is the same


Yes, but *understanding* the code (for its performance) is the most
important alternative of all. Doing so allows you do this:

char * QEDstrstr (const char * d0, const char * d1) {
int i, j;
char c0, c1;

/* peel off case: strstr (*, "") */
if ('\0' == (c0 = d1[0])) return (char *) d0;

/* peel off case: strstr (*, one-character-string) */
if ('\0' == (c1 = d1[1])) {
char * d = (char *) d0;
for (; c0 != *d; d++) {
if ('\0' == *d) return NULL;
}
return d;
}

for (i = 0;; i++) {

/* Unrolled (once) first character test */
loop0:;
if (c0 != d0[i]) {
if ('\0' == d0[i]) return NULL;
if (c0 != d0[i+1]) {
if ('\0' == d0[i+1]) return NULL;
i+=2;
goto loop0;
}
i++;
}

/* Second character test */
if (c1 != d0[i+1]) continue;

/* Check beyond first two characters */
for (j = 2; d1[j] == d0[i+j]; j++) {
if ('\0' == d1[j]) return (char *) (d0 + i);
}
if ('\0' == d1[j]) return (char *) (d0 + i);
}
}

This achieves very close to the GNUstrstr performance (from my own
testing, on multiple compilers), while being somewhat more readable.
The trick, of course, was to unroll (the core reason why the GNU
version is faster), and peel off one case (which is just a standard
thing I do, as necessary). Its commented, and therefore even more
maintainable than the Sun version.

Now, would you say, this sort of optimization was not worth it, because
I cannot possibly optimize for multiple platforms/compilers at once
(even though I did just that)? That the micro-technique (unrolling) is
not worth it because you're hoping that your compiler will do it for
you (only one I tried succeeded in doing that)?

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Jun 19 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: pieter.breed | last post by:
Hi All, The company I work for has traditionally used COM/ActiveX for the solutions that it provides. We are in the process of moving to .NET and a few applications have been written in VB.NET...
4
by: womanontheinside | last post by:
I have a library which was written in C, you call a function, it provides the result by a callback to specific function names. I am trying to wrap the calls to this inside a class, but this causes...
3
by: K.S.Liang | last post by:
Hi all, 1> If there are more than one dynamic linking libraries in the file system, how do I know which one is loaded into system? Any C library or system call can tell me which *.so or *.sl is...
19
by: Deniz Bahar | last post by:
Hi, I would like to call one of my functions the exact name as an existing C library function (for example K&R2 exercises asks me to make an atof function). If I don't include the header with...
3
by: Manny Silva | last post by:
Hi, I would like to create a static library that uses and in effect adds to another static library. I could simply add functionality to the existing library, but functionally it doesn't really...
1
by: Jim | last post by:
Have fully operational software package developed on VB.NET that worked until Jan 1 2003, with early stage deployments on Oct 10, Oct 23, Nov 11, Dec 12 and Dec 30. When attempted final...
10
by: mwt | last post by:
So in a further attempt to learn some Python, I've taken the little Library program (http://groups.google.com/group/comp.lang.python/browse_thread/thread/f6a9ccf1bc136f84) I wrote and added...
10
by: Julian | last post by:
I get the following error when i try to link a fortran library to a c++ code in .NET 2005. LINK : fatal error LNK1104: cannot open file 'libc.lib' the code was working fine when built using...
0
by: JosAH | last post by:
Greetings, the last two article parts described the design and implementation of the text Processor which spoonfeeds paragraphs of text to the LibraryBuilder. The latter object organizes, cleans...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.