|
From: Ioannis V. <no...@ya...> - 2001-10-08 05:09:10
|
It is great you are interested for the reality of the things. :) At first your code had some errors (for 100% ansi standard) and omitions (you forgot #include <iostream> for example) which i fixed. I did not dare to see your code and think how exactly your algorithm work but the results showed the C++ versions to be slower than C's. I attach you your code fixed as old.cpp. I wrote my code using the standard library sorting function thus applying the same algorithm to both stuff. The code is attached as=20 temp.cpp. The results are: C:\c>temp begin: 2002 cend: 24885 cppend: 47558 char * sorting speed: 22883 std::string sorting speed: 22673 As you see the string speed here is smaller than char *. :) In less elements the opposite happened. In both situations the difference is small. In worse case scenario C++ code must function at the same speed as C's code. And this because C++ was designed for 0 space and time overhead. Also as i said you before when you do not use string and use char pointers you end up using more code than those provided by built in member functions(=3Dspace overhead). Also you end up with time overhead! And this because function calls are more expensive than member function calls, and especially in a good compiler where template member functions are inlined. So in practice using C++ mechanisms is far better than using C's because of the less head aches, clearer code, more bug free code (you can end up copying things after the end of a char array for example, but not in a string), less code, far easier maintainability and probably better speed! As i told before take also a look at:=20 http://www.research.att.com/~bs/esc99.html The reason that your code produces big differences is obviously bugs. The algorithms aren't applied fairly the same at both worlds, e.t.c.. Also i am glad to discuss with you these matters. I am waiting fro your reply on the issue. I am sending my reply in the list because there are others who will benefit from this discussion. :) Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.f2s.com * Alternative URL: http://run.to/noicys > -----Original Message----- > From: Jason Hardman [mailto:jas...@bt...] > Sent: Monday, October 08, 2001 4:44 AM > To: Ioannis Vranos > Subject: Re: [Dev-C++] Help with functions >=20 >=20 > Hi again, >=20 > I know this is an old issue but I wanted to demonstrate a > point. I understand the points you were making about function=20 > calls versus method calls and how the varying overheads are=20 > minimal. However I wanted to show you that C string versus=20 > stl::string does provide a lot of difference. >=20 > If you take a look at the attached code you will find 4 > different versions of the sort algorithm functions I had=20 > posted earlier. The two original ones using C style strings,=20 > slightly redesiged to not modify the input string, and then=20 > two new version of the same algorithms, using the stl::string=20 > class. If you run the program it will report how long it took=20 > a certain number of iterations of each function. The results=20 > have been quite interesting on my computer, and I assume they=20 > will be similar on yours. I recomend that you run it with and=20 > without compiler optimisations switched on. The compiler I=20 > use is MinGW, so I don't know how other compilers will=20 > perform, but I don't think the results will change a great=20 > deal in others. Be prepared to wait a bit, my computer is a=20 > PIII 750 and the whole program when fully optimised takes=20 > about 20 secs, unoptimised is much longer. >=20 > The reason I have done this is out of an interest in such > things, and also just sheer bloody mindedness. I haven't=20 > meant to do it to be argumentative, but meerly to illustrate=20 > an issue. Hope you don't mind? I have actualy enjoyed=20 > tinkering around with the program as I haven't had much=20 > opportunity in the past to analyse speeds in programs. There=20 > are probably a few more optimisations I could introduce,=20 > especially in both V1 functions, mainly by using pointers and=20 > iterators respectivly. >=20 > This doesn't mean that I recomend C strings over stl::string > objects, but it does demonstrate that a person should choose=20 > wisely depending upon the task they will be performing. I=20 > welcome any thoughts you have. >=20 > I will note that the case I have demonstrated isn't of any > import unless working with a very large set of data, when=20 > execution time is critical. Also the string I have supplied=20 > as a test case is one that tries to provide a representative=20 > case for normal use of the algorithms. It is interesting (to > me) how using different input strings changes the results,=20 > mainly when comparing the two algorithms. Try strings of=20 > differing lengths, and vary how unsorted the strings are,=20 > i.e. give it a string that doesn't need any sorting. >=20 > Thanx for putting up with me, Jason. >=20 > ----- Original Message ----- > From: "Ioannis Vranos" <no...@ya...> > To: "Dev-C++" <dev...@li...> > Sent: Friday, October 05, 2001 6:45 AM > Subject: RE: [Dev-C++] Help with functions >=20 > The notion that the C subset is fastest than C++ is a myth. > See http://www.research.att.com/~bs/esc99.html and=20 > http://www.research.att.com/~bs/crc.pdf >=20 > C++ string is a > typedef of basic_string<char> type that is: >=20 > typedef basic_string<char> string; >=20 > basic_string is a template class and template classes tend to > expanded inline when compiled. Also a good compiler creates=20 > instances only of the member functions of a template class=20 > which are used and ignores the rest. Thus since the code is=20 > expanded inline in the application you have 0 runtime overhead. >=20 >=20 >=20 >=20 |