LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-14-2005, 07:45 AM   #1
pablowablo
Member
 
Registered: Apr 2004
Posts: 131

Rep: Reputation: 15
pointer notation vs array notation?


According to some text I'm reading *(array + i) is much faster than array[i] since the following has to be done on the latter: &array[0] + i * sizeof(TYPE)

I was wondering if pointer notation is really faster than array notation. Could anyone verify this for me? The reason that I'm having some doubt about this is because the compiler is smart enought to know the size of the element in pointer arithmetic but why not in array notation?


Thanks!
 
Old 03-14-2005, 09:02 AM   #2
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Re: pointer notation vs array notation?

Quote:
Originally posted by pablowablo
According to some text I'm reading *(array + i) is much faster than array[i] since the following has to be done on the latter: &array[0] + i * sizeof(TYPE)

I was wondering if pointer notation is really faster than array notation. Could anyone verify this for me? The reason that I'm having some doubt about this is because the compiler is smart enought to know the size of the element in pointer arithmetic but why not in array notation?
Thanks!

You can test it by yourself if you want, by creating loops, but I wouldn't bother with those kind of things, as long as you use -O2 both will run in the same time.
 
Old 03-14-2005, 09:14 AM   #3
zeropash
Member
 
Registered: Apr 2003
Location: Bangalore,India
Distribution: FC2, RHES, RH9, FC3, FC1, Slackware 3.0
Posts: 208

Rep: Reputation: 30
hmm lets see
int arr[20];

int func1(int i) {
*(arr+i) = 10;
}

int func2(int i) {
arr[i] = 10;
}

int main() {
func1(9);
func2(9);
}
compile this and run it using gdb
disassemble func1 gives
Dump of assembler code for function func1:
0x08048334 <func1+0>: push %ebp
0x08048335 <func1+1>: mov %esp,%ebp
0x08048337 <func1+3>: mov 0x8(%ebp),%eax
0x0804833a <func1+6>: movl $0xa,0x8049580(,%eax,4)
0x08048345 <func1+17>: leave
0x08048346 <func1+18>: ret
0x08048347 <func1+19>: nop
disassemble func2 gives
Dump of assembler code for function func2:
0x08048348 <func2+0>: push %ebp
0x08048349 <func2+1>: mov %esp,%ebp
0x0804834b <func2+3>: mov 0x8(%ebp),%eax
0x0804834e <func2+6>: movl $0xa,0x8049580(,%eax,4)
0x08048359 <func2+17>: leave
0x0804835a <func2+18>: ret
0x0804835b <func2+19>: nop
End of assembler dump.

Now spot the difference (I cannot)
you can see that the compiler produces the exact same code. so dont expect any change in performance .
you can check on other arch too.

Last edited by zeropash; 03-14-2005 at 09:16 AM.
 
Old 03-14-2005, 09:24 AM   #4
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
zeropash's idea is a good one. If you want to know if two different ways produces the exact same code, simply study the assembler output!
 
Old 03-14-2005, 09:38 AM   #5
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Of course all those things depend from the compiler. Maybe older compilers used to output different code for those 2 things but not any more.
 
Old 03-14-2005, 12:34 PM   #6
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
I think your text is simply broken because when you do pointer arithmetic it multiplies by the sizeof the thing pointed to by the pointer anyway.

Code:
int *foo;
...
*(foo + i) = blah;
is equivalent to
Code:
int *foo;
...
*(int*)(((char*)foo)+sizeof(*foo)*i) = blah;
which makes it exactly the same as the other syntax.

On the other hand, something along these lines which might make your code faster, particularly without optimizations turned on, is doing

Code:
char *p;
for (p = s; *p != '\0'; ++p) {
    ...
}
instead of
Code:
int i;
for (i = 0; s[i] != '\0'; ++i) {
    ...
}
I'd bet that optimizations turns the second form into the first.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
hex notation to a C compiler rhoyerboat Programming 5 02-16-2005 09:42 AM
Is this a standard notation in inux? ray5_83 Linux - General 3 08-04-2004 07:15 AM
music notation software American Psycho Linux - Software 2 03-23-2004 09:49 PM
Music Notation software Micro420 Linux - Software 9 09-06-2003 03:29 PM
Scientific notation for C Chijtska Programming 4 02-19-2002 09:24 AM

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

All times are GMT -5. The time now is 01:30 AM.

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