Raised This Month: $51 Target: $400
 12% 

[help] getting 1º char in text


Post New Thread Reply   
 
Thread Tools Display Modes
sslice
Senior Member
Join Date: Feb 2005
Location: Texas, USA
Old 04-30-2007 , 21:56   Re: [help] getting 1º char in text
Reply With Quote #11

Indeed.. text[0] gets the first character of a string, or if you want, *text, but text[0] is generally easier to read.
sslice is offline
MysteryMeat
Junior Member
Join Date: Jan 2006
Old 04-30-2007 , 23:38   Re: [help] getting 1º char in text
Reply With Quote #12

Won't text[0] translate into "address of text + (0 * 1)"? Multiplication is relatively CPU intensive, you will have much better performance with just *text. How big of a difference depends on how often the code runs.
MysteryMeat is offline
BAILOPAN
Join Date: Jan 2004
Old 04-30-2007 , 23:48   Re: [help] getting 1º char in text
Reply With Quote #13

Believe it or not, the compiler is capable of multiplying 0 times 1 fairly quickly without generating any code.
__________________
egg
BAILOPAN is offline
sslice
Senior Member
Join Date: Feb 2005
Location: Texas, USA
Old 04-30-2007 , 23:49   Re: [help] getting 1º char in text
Reply With Quote #14

The compiler is smart enough to figure it out itself, and even if you did test[2] no multiplication is used. The only thing that happens here is addition and dereferencing.

Note: text[2] can be rewritten as *(text + 2)
sslice is offline
MysteryMeat
Junior Member
Join Date: Jan 2006
Old 05-01-2007 , 01:35   Re: [help] getting 1º char in text
Reply With Quote #15

Wasn't sure if the compiler would take care of that or not, didn't really want to create the assembly code to find out (not sure how anymore).

[Edit] Didn't even consider the fact that it was a hardcoded value and the compiler would precalculate the offset. I'll shut up now

Last edited by MysteryMeat; 05-01-2007 at 02:00.
MysteryMeat is offline
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 05-01-2007 , 04:02   Re: [help] getting 1º char in text
Reply With Quote #16

Quote:
Originally Posted by MysteryMeat View Post
Won't text[0] translate into "address of text + (0 * 1)"? Multiplication is relatively CPU intensive, you will have much better performance with just *text. How big of a difference depends on how often the code runs.
It's almost always safe to assume that the compiler will do trivial optimizations like this one (and by the way, multiplication, even floating-point multiplication is pretty cheap on today's processors. On the other hand, float division is/used to be slower, that's why some programmers first compute 1.0/length and then multiply the vector's components with that value when normalizing a vector. note that in most 3D applications, normalizing is a operation which is done VERY often, so people try to optimize even that. how often is a console command invoked?). What the compiler can't do is optimize a bubble sort into a quick sort.
In other words: in my opinion, it is usually good enough to concentrate on a good choice of data structures and efficient algorithms and writing readable code instead of trying to do small-scale stuff like this
The next thing would be trying to avoid pipeline flushes and cache misses.
__________________
hello, i am pm

Last edited by PM; 05-01-2007 at 04:08.
PM is offline
sn4k3
Senior Member
Join Date: Nov 2005
Old 05-01-2007 , 14:02   Re: [help] getting 1º char in text
Reply With Quote #17

thats exemples giveme conversions errors on compile
sn4k3 is offline
Send a message via MSN to sn4k3
devicenull
Veteran Member
Join Date: Mar 2004
Location: CT
Old 05-01-2007 , 14:15   Re: [help] getting 1º char in text
Reply With Quote #18

Quote:
Originally Posted by sn4k3 View Post
thats exemples giveme conversions errors on compile
Honestly... if you can't figure out how to get the first item in an array, you probably shouldn't be releasing plugins..
__________________
Various bits of semi-useful code in a bunch of languages: http://code.devicenull.org/
devicenull is offline
Keeper
Senior Member
Join Date: Nov 2006
Old 05-01-2007 , 14:26   Re: [help] getting 1º char in text
Reply With Quote #19

The problem, I bet is he's starting with a const char*.
Keeper is offline
BAILOPAN
Join Date: Jan 2004
Old 05-01-2007 , 14:44   Re: [help] getting 1º char in text
Reply With Quote #20

How is that a problem?
__________________
egg
BAILOPAN is offline
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 On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:58.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode