|
From: Ioannis V. <no...@ya...> - 2001-10-08 07:58:36
|
People sorry for the reposting but i made some important expression mistakes. 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: 1992 cend: 24515 cppend: 47017 char * sorting time: 22523 std::string sorting time: 22502 As you see the string sorting time 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=20 > understand the points you were making about function calls versus=20 > method calls and how the varying overheads are minimal. However I=20 > wanted to show you that C string versus stl::string does provide a lot > of difference. >=20 > If you take a look at the attached code you will find 4 different=20 > versions of the sort algorithm functions I had posted earlier. The two > original ones using C style strings, slightly redesiged to not modify=20 > the input string, and then two new version of the same algorithms,=20 > using the stl::string class. If you run the program it will report how > long it took a certain number of iterations of each function. The=20 > results have been quite interesting on my computer, and I assume they > 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=20 > also just sheer bloody mindedness. I haven't meant to do it to be=20 > argumentative, but meerly to illustrate an issue. Hope you don't mind? > I have actualy enjoyed tinkering around with the program as I haven't=20 > had much opportunity in the past to analyse speeds in programs. There > 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,=20 > but it does demonstrate that a person should choose wisely depending=20 > upon the task they will be performing. I welcome any thoughts you=20 > have. >=20 > I will note that the case I have demonstrated isn't of any import=20 > unless working with a very large set of data, when execution time is=20 > critical. Also the string I have supplied as a test case is one that=20 > tries to provide a representative case for normal use of the=20 > algorithms. It is interesting (to > me) how using different input strings changes the results, > 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=20 > 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 instances only of=20 > the member functions of a template class which are used and ignores=20 > the rest. Thus since the code is expanded inline in the application=20 > you have 0 runtime overhead. >=20 >=20 >=20 >=20 |