Integer and pointer

Hi everyone,
I have a doubt regarding assignment of integer using pointer.

1 :- int_i = *int_ptr1;
2 :- int_i = *uchar_ptr;
3 :- int_j = int_k;

4:- *int_ptr2 = int_i;
5:- *int_ptr2 = int_j;

What are the difference among 1,2 and 3 ? Incase of 1 and 3 all the variables are
integer type. But why 3 performs faster than 1 and 2 performs faster than 1 ?
(Inside a huge time loop)

3 and 4 are almost same. But if i use 1,4 and 1,5 then 1,5 is faster than 1,4.
Why there is a speed difference in assignment of integer type ?

thanks.
Last edited on
3 is probably performing faster than 1 because 1 has to do the extra work of dereferencing the pointer.

I'm not completely sure why 2 would perform faster than 1. If there was a difference I would expect 2 to be slower because it has to convert from unsigned char to int. You say you are using this inside a huge loop so I guess the pointers are pointing to data in an array and the pointers are incremented for each iteration. Am I right? In that case it probably has to do with the memory cache. unsigned char is smaller than int so it will generate cache misses less frequently. That is probably what makes 2 look faster.


You are saying that
1
2
int_i = *int_ptr1;
*int_ptr2 = int_j

is faster than
1
2
int_i = *int_ptr1;
*int_ptr2 = int_i;
.
Maybe it can do some of 1,5 in parallel because it is operating on two different variables (int_i, int_i). In the case of 1,4 it has to fully read the value from *int_ptr1 before it can start writing it to *int_ptr2.
Last edited on
Thanks for reply.

May be what you told about 1,2 and 3 is correct.But if i replace 1 with

*(uchar_ptr + *int_ptr1) (no assignment) then it will behave same as 3.
If pointers are doing extra work then it should take more time than 3.

In case of 4 and 5 both will read same amount of memory(sizeof(int)).

Below i gave my working code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

    int ht = 504,wt = 474,H = 60,W = 56;

    int i,j,m,n,k,sum;

    unsigned char *i_ptr = NULL,*g_ptr_u = NULL;

    int *g_ptr = NULL,*r_ptr =NULL;



     for(k = 0; k < 3360; k++)

    {

        i_ptr = iData+boffset[k];  // uchar iData[wt X ht] , int boffset[3360]

        for(i = 0; i < 16; i++)

        {

            r_ptr = rs;  // inr rs[24]

            g_ptr = grids[i];  /int grids[16][24*24]

            //g_ptr_u = grids_u[i];

            for(m = 0; m < 24; m++)

            {

                //rs[i] = 0;

                sum = 0;

                g_ptr += 8;

                //g_ptr_u += 8;

                for(n = 0; n < 8;n++)

                {

                    //sum += *(i_ptr+ n); // st 1

                    sum += *(i_ptr+ *g_ptr); // st 2

                    //sum += *(i_ptr+ *g_ptr_u); st 3

                    g_ptr++;

                    //g_ptr_u ++;

                }

                g_ptr += 8;

                //g_ptr_u += 8;

                //*r_ptr++ = n;   //st 4
                *r_ptr++ = sum; //st 5

            }

        }

    }



This is my code and i want to optimize it.
I test this code in embedded system(ARM 9 processor, linux 2.6.30).
The execution time :-

e1 :- sum = uchar_ptr + int st 1
e2 :- sum = uchar_ptr + int_ptr st 2
e3 :- sum = uchar_ptr + uchar_ptr st 3



e4 :- st 4
e5:- st 5

e1 and e4 36 millisecond e1 and e5 183 millisecond

e2 and e4 159 millisecond e2 and e5 1878 millisecond

e3 and e4 34 millisecond e3 and e5 296 millisecond

From here you can sea the effect of st4 and st5.

1 will behave as st 2.
2 will behave as st 3.
3 will behave as st 3.

My actual case is e2 and e5 and i want to reduce the time.
Please help me to optimize it.

thanks
Topic archived. No new replies allowed.