6 Replies - 6541 Views - Last Post: 20 December 2011 - 08:30 AM Rate Topic: -----

#1 livium   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 554
  • Joined: 21-December 08

CString vs wstring

Posted 20 December 2011 - 03:03 AM

Hello!

I want to know which one is faster: CString or wstring?

I must use wstring in my project which behaves the same way as a CString.
So I created my own MyString class which looks like the one below:
Please don't ask why I must use wstring instead of CString.

#include <string>
using namespace std;

class MyString
{
	
	public:
			MyString();
			MyString(const wchar_t * pw);
			MyString(wchar_t wch);
			MyString(const MyString&);
			MyString& operator=(const MyString& mstr);
			MyString& operator=(const wchar_t* pw);
			//~MyString();
			bool operator==(const MyString& wstr) const;
			wchar_t operator[](size_t  pos) const;
			bool operator<(const MyString& mstr) const;

			friend MyString operator+(const wchar_t* pw, MyString mstr);

			MyString operator+(const MyString& mstr);
			bool operator!=(const wchar_t* pw);

			int Compare(const MyString& mstr) const;
			void Append (const MyString& mstr);
			size_t Insert(size_t pos, wchar_t wch);
			size_t Delete(size_t pos);
			size_t Delete(size_t pos, size_t count);
			void SetAt(size_t pos, wchar_t wch);
			MyString Left(size_t n) const;
			MyString Right(size_t n) const;
			size_t GetLength() const;
			size_t Find(wchar_t wch) const;
			MyString Mid(size_t pos) const;
			void Empty();
			const wchar_t* GetString() const;
			const wchar_t* GetBuffer() const;

			
	private:
			wstring _word;
			
};



You can see that this class has the same functions as CString class.
Unfortunatly, I tested this class on my project and it is slower than CString at about 20% and I whonder why is that?

Is it because wstring is slower than CString or because I must define some functions inline?

Please help me!

This is so important to me.
My carrer depends on this, i'm not kidding!

This post has been edited by livium: 20 December 2011 - 03:10 AM


Is This A Good Question/Topic? 0
  • +

Replies To: CString vs wstring

#2 oscode   User is offline

  • D.I.C Regular

Reputation: 109
  • View blog
  • Posts: 257
  • Joined: 24-October 10

Re: CString vs wstring

Posted 20 December 2011 - 06:39 AM

It depends on the implementation and which hardware you're running it on. Luckily, you're a developer with the appropriate developer tools and hardware, so why don't you test it yourself? Also you don't specify which functionality you want to be faster, or which functionality is 20% faster. Allocation, deallocation, searching, inserting, removing, copying, converting?

This post has been edited by oscode: 20 December 2011 - 06:40 AM

Was This Post Helpful? 1
  • +
  • -

#3 livium   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 554
  • Joined: 21-December 08

Re: CString vs wstring

Posted 20 December 2011 - 06:50 AM

Well, inserting, deleting, SetAt are the most used methods.

If I make them inline than the performance is still the same compared to CString.

For example I made Insert inline and I don't see any improvement in performance.
Was This Post Helpful? 0
  • +
  • -

#4 oscode   User is offline

  • D.I.C Regular

Reputation: 109
  • View blog
  • Posts: 257
  • Joined: 24-October 10

Re: CString vs wstring

Posted 20 December 2011 - 07:05 AM

You're going to need to know the performance requirements for each operation. Some will have to suffer for the benefit of others, as will the memory footprint. If you HAVE to use Microsoft's wstring and it's slower than CString, it's not your problem. You should try building your code with all appropriate optimizations enabled in the compiler settings. The inline keyword doesn't (shouldn't) really do anything on modern optimizing compilers.
Was This Post Helpful? 1
  • +
  • -

#5 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: CString vs wstring

Posted 20 December 2011 - 07:27 AM

You could roll your own, of course... I mean, if you know the test harness, you should be able to beat most implementations by knowing the requirements up front.

Also, wstring is comparable to CStringW. If you're comparing to CStringA, you're not being fair.
Was This Post Helpful? 0
  • +
  • -

#6 livium   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 554
  • Joined: 21-December 08

Re: CString vs wstring

Posted 20 December 2011 - 08:26 AM

what is the difference between CStringW and CStringA?

I mean CStringA is CString?
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg   User is online

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: CString vs wstring

Posted 20 December 2011 - 08:30 AM

CStringA == CString ASCII, CStringW == CString Wide Characters. See this link: CString.



Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1