LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-22-2011, 11:24 PM   #16
xuancong
LQ Newbie
 
Registered: Jul 2010
Posts: 18

Original Poster
Rep: Reputation: 0

Quote:
Originally Posted by dwhitney67 View Post
See answer below:
Code:
void toupper(std::string& s)
{
   for (size_t i = 0; i < s.length(); ++i)
   {
      s[i] = ::toupper(s[i]);
   }

#ifdef CRAP
   char *p = (char*)s.c_str();
   while(*p){*p=toupper(*p);p++;}
#endif
}
Thanks a lot! That works! -

But I am always worried that using s[i] for string and vector is slow and inefficient because it calls the string::[] operator function every time to retrieve the address of the corresponding character in the string. I'm not sure how fast g++ implements this. Perhaps someone can come out with a speed test to compare the exec speed of modifying s[i] for {char*s} and {string/vector s}. That would fully settle my perhaps unnecessary worry which results in my casting const char* to char*!


Quote:
@johnsfines: xuancong has very bad track record of this. Hopefully he'll take a more appropriate tone in future posts.
I'm really sorry for that, I always gets too agitated upon encountering these kinds of problems, I'll be more careful in the future.

Last edited by xuancong; 02-22-2011 at 11:54 PM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 02-22-2011, 11:58 PM   #17
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by xuancong View Post
But I just heard of that using s[i] for string and vector is slow and inefficient
That's called "premature optimization".

Instead of believing that something is slow, you should measure actual performance using profilers (linux has gprof for that). Code needs to be optimized for speed only if you identified it as a bottleneck using a profiler. With modern CPU speeds, unless your APP processes billions of strings per second, difference between [] version and pointer version will not be noticeable, and it is possible that (depending on compiler) pointer version will be slower than []-based or iterator-based version.

Before "optimizing" something, you should profile (with gprof or another software) application and find real bottleneck. Without measurements it is easy to spend hours optimizing routine that is never called. Typically slowest operations are memory allocation (calling new/delete millions of times per second), not [] operators. However, your app currently is too small (only two routines), it doesn't run for hours, so there is nothing to optimize yet.

Last edited by SigTerm; 02-23-2011 at 12:19 AM.
 
Old 02-23-2011, 12:43 AM   #18
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,222

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by SigTerm View Post
With C++ casting you'll get same behavior.... IMO const_cast should be used as a last resort, and preferably only somewhere within a class.
Not the point. Xuancong had failed to RTFM and therefore had no idea that he was casting a const pointer to non-const. Therefore, if he were using C++-style casting, then he would not have used a const_cast. He would instead have tried to use a static_cast, and its failure would have made him realize what he was doing wrong. If he had used a const cast, he would have known what he was doing and therefore would not have started this thread.

Quote:
Originally Posted by SigTerm View Post
With modern CPU speeds, unless your APP processes billions of strings per second, difference between [] version and pointer version will not be noticeable, and it is possible that (depending on compiler) pointer version will be slower than []-based or iterator-based version.
In addition to this and your other very good points, I would add that:

a) the code for the indexing operator may well be inlined by the compiler
b) if you actually were calling the indexing operator billions of times per second, the instructions would almost certainly be cached by the CPU

In both cases, using the indexer would be as fast as the pointer version.

EDIT: the explanation for xuancong's follow-up post is that an earlier version of this one mentioned using std::transform to convert strings to uppercase.

Last edited by dugan; 02-23-2011 at 01:36 AM.
 
Old 02-23-2011, 01:29 AM   #19
xuancong
LQ Newbie
 
Registered: Jul 2010
Posts: 18

Original Poster
Rep: Reputation: 0
Smile

Code:
void toupper(std::string &s) {
std::transform(s.begin(), s.end(), s.begin(), (int (*)(int))std::toupper);}
Thanks! I found it. How come C++ language specification does not include a function for convert strings to upper or lowercase, this is very often used, right?
 
Old 02-23-2011, 07:12 AM   #20
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by xuancong View Post
How come C++ language specification does not include a function for convert strings to upper or lowercase, this is very often used, right?
I guess that it is not include is because it is a complicated algorithm best suited to specialised libraries.

If you are only considering ACSII then it is simple, but UNICODE means that the requirements are much more extensive than that, if it is to be done properly.
 
Old 02-23-2011, 08:32 AM   #21
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,222

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by xuancong View Post
How come C++ language specification does not include a function for convert strings to upper or lowercase
The Boost string algorithm library, which includes to_upper() and to_lower(), has been proposed for C++0x.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Template algorithm for std::vector and std::string CartmanYO Programming 4 09-08-2010 09:56 AM
Compiler error: "Conversion from 'bool' to non-scalar type 'std::string' requested" Kenny_Strawn Programming 6 03-06-2010 08:22 PM
Assigning custom string to std::string in C++ jh7777 Programming 4 10-13-2009 12:02 AM
Inconsistent handling of attachments in Thunderbird statguy Slackware 12 01-11-2009 11:06 AM
C++ std::string to int Slaxx Programming 1 10-30-2004 10:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration