Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   **FIRST EMAIL**/Java and Orbit Balls *****JAVA DISCUSSION*** (http://www.chiefdelphi.com/forums/showthread.php?t=77418)

The Lucas 17-05-2009 23:54

Re: **FIRST EMAIL**/Java and Orbit Balls
 
Quote:

Originally Posted by Alan Anderson (Post 860009)
I don't understand the point (!) of your example. What part of it should I focus on? None of it appears to show me a reason for preferring to use explicit pointers.

I generated assembly for both of the examples with the relevant instructions bolded

Quote:

Originally Posted by Chris27 (Post 859768)
Code:

1 #include <stdlib.h>
 2
 3 int main()
 4 {
 5  int *array = malloc(10 * sizeof(int));
 6  int *ptr;
 7  int i;
 8
 9  for(i = 0; i < 10; i++)
10    ptr = array + i;
11
12  return 0;
13 }



Code:

.L2:
        cmpl    $9, -12(%ebp)
        jg      .L3
        movl    -12(%ebp), %eax
        sall    $2, %eax
        addl    -4(%ebp), %eax
        movl    %eax, -8(%ebp)

        leal    -12(%ebp), %eax
        incl    (%eax)
        jmp    .L2
.L3:

Quote:

Originally Posted by The Lucas (Post 859797)
Code:

1 #include <stdlib.h>
 2
 3 int main()
 4 {
 5  int *array = malloc(10 * sizeof(int));
 6  int *ptr = array;
 7  int i;
 8
 9  for(i = 0; i < 10; i++)
10    ptr++;
11
12  return 0;
13 }



Code:

.L2:
        cmpl    $9, -12(%ebp)
        jg      .L3
        leal    -8(%ebp), %eax
        addl    $4, (%eax)

        leal    -12(%ebp), %eax
        incl    (%eax)
        jmp    .L2
.L3:

So 4 instructions are necessary for ptr = array + i; and only 2 instructions for ptr++; in this assembly language.

Quote:

Originally Posted by Alan Anderson (Post 860009)
Isn't pass by reference exactly what Java lets you do in such cases? I guess I'm completely failing to understand what you're trying to explain here.

I Googled "java swap" and this site explains it pretty well. Basically when you pass by reference to a function in Java it gives the function a new (different) reference to the object and there is no way to change the original reference in the passing function. In C++, the pointer is like an absolute reference (memory location) that is the same everywhere and you can even use pointers to pointers (no references to references in Java).

Chris27 18-05-2009 00:39

Re: **FIRST EMAIL**/Java and Orbit Balls
 
Quote:

Originally Posted by The Lucas;860118
So 4 instructions are necessary for [B
ptr = array + i;[/b] and only 2 instructions for ptr++; in this assembly language.

So what's your point? Of course an expression that contains multiple variables will generally be more complex then an expression that contains a single variable. ptr = (&(ptr[0])) + 1 will again compile to the same assembly. Array indexing is just an encapsulation for pointer arithmetic as I explained earlier. Any pointer arithmetic expression will have a matching array index expression and both of these expressions will compile to the exact same instructions.

Quote:

I Googled "java swap" and this site explains it pretty well. Basically when you pass by reference to a function in Java it gives the function a new (different) reference to the object and there is no way to change the original reference in the passing function. In C++, the pointer is like an absolute reference (memory location) that is the same everywhere and you can even use pointers to pointers (no references to references in Java).
If you want to change a reference to an object, just have the method return a new reference. If you want a series of references, just encapsulate the references in objects. If you absolutely need to have access to the virtual address of an object, then Java just isn't the right language for the job.

Here is swap with references:

Code:

public class MyInt {
       
        int data;
        public MyInt(int x)
        {
                this.data = x;
        }
       
        public static void swap(MyInt a, MyInt b)
        {
                a.data ^= b.data;
                b.data ^= a.data;
                a.data ^= b.data;
        }

}


The Lucas 18-05-2009 03:05

Re: **FIRST EMAIL**/Java and Orbit Balls
 
Quote:

Originally Posted by Chris27 (Post 860124)
ptr = (&(ptr[0])) + 1 will again compile to the same assembly.

You can't dereference (&) and do pointer arithmetic like that in Java.

Quote:

Originally Posted by Chris27 (Post 860124)
Array indexing is just an encapsulation for pointer arithmetic as I explained earlier. Any pointer arithmetic expression will have a matching array index expression and both of these expressions will compile to the exact same instructions.

There is no matching array index expression for this:

Code:

ptr = 365;

Array indexing requires a symbol (base of array) and a type (size of element). When an integer can be cast to a pointer, pointer arithmetic can be done without an array.

Quote:

Originally Posted by Chris27 (Post 860124)
If you want to change a reference to an object, just have the method return a new reference. If you want a series of references, just encapsulate the references in objects. If you absolutely need to have access to the virtual address of an object, then Java just isn't the right language for the job.

Here is swap with references:

Code:

public class MyInt {
       
        int data;
        public MyInt(int x)
        {
                this.data = x;
        }
       
        public static void swap(MyInt a, MyInt b)
        {
                a.data ^= b.data;
                b.data ^= a.data;
                a.data ^= b.data;
        }

}


I know you can create a new class, a similar example was on the page I linked. I just said it was simpler in C. Creating new classes or returning multiple references doesn't sound simple to me.

Again, I understand both languages and the reasons why Java doesn't allow explicit pointers (source of bugs, reference counting for garbage collecting, sercurity, etc..).

Lets revisit how this whole conversion happened (with me paraphrasing)
Billy: Yuck Java. Pointers ftw :D
Jared: What cant you do with Java references?
Me: Pointer Arithmetic.
Jared: Granted, but you better have a good reason.
<Long posts involving example code by Chris, me and Alan>
I thought twice before pressing Submit Reply on my first post because I knew it wouldn't be my last on this thread;) Programming debates are interesting (for me atleast) but time consuming. Maybe I should have just let Billy reply or learned from C++ vs LV threads.

Alan Anderson 18-05-2009 09:30

Re: **FIRST EMAIL**/Java and Orbit Balls
 
Quote:

Originally Posted by The Lucas (Post 860130)
You can't dereference (&) and do pointer arithmetic like that in Java.

Right. The whole discussion here is based on the lack of pointer arithmetic in Java. But I'm still trying to find out why some think that pointer arithmetic is more efficient than using arrays. My understanding is that they compile to exactly the same thing, since in C a[i] is just syntactical sugar for *(a+i). I used to confuse people by writing i[a] instead, which gives exactly the same result.
Quote:

There is no matching array index expression for this:
Code:

ptr = 365;

Without significant context, I'm at a loss to understand why you'd want to write it. With the exception of using memory-mapped I/O, I have yet to see any examples anywhere where using absolute pointers gives any improvement in efficiency over using arrays.

Bharat Nain 18-05-2009 10:24

Re: **FIRST EMAIL**/Java and Orbit Balls
 
Quote:

Originally Posted by Alan Anderson (Post 860145)
Right. The whole discussion here is based on the lack of pointer arithmetic in Java. But I'm still trying to find out why some think that pointer arithmetic is more efficient than using arrays.


More importantly, how much does it matter in our application? What difference will it make?

Jared Russell 18-05-2009 10:39

Re: **FIRST EMAIL**/Java and Orbit Balls
 
Quote:

Originally Posted by Bharat Nain (Post 860149)
More importantly, how much does it matter in our application? What difference will it make?

Because if you don't use pointer arithmetic, you can't be a real programmer. :cool:

(I know that's not Brian's argument, but it is a good excuse to post one of my all time favorite programming urban legends)

Greg Needel 18-05-2009 10:52

Re: **FIRST EMAIL**/Java and Orbit Balls *****JAVA DISCUSSION***
 
I split this conversation into 2 threads into a java discussion and an orbit ball one.

The Lucas 18-05-2009 13:53

Re: **FIRST EMAIL**/Java and Orbit Balls
 
Quote:

Originally Posted by Greg Needel (Post 860156)
I split this conversation into 2 threads into a java discussion and an orbit ball one.

Sorry for the extra work Greg but it was bound to happen.

Quote:

Originally Posted by Alan Anderson (Post 860145)
Right. The whole discussion here is based on the lack of pointer arithmetic in Java. But I'm still trying to find out why some think that pointer arithmetic is more efficient than using arrays.

It is clear I haven't been a targeting my statements well.

Alan, to show that pointer arithmetic can be more efficient than array indexing, I used ptr++ (cant use in Java) instead of array indexing in Chris' example loop. The generated assembly showed:
Quote:

Originally Posted by The Lucas (Post 860118)
So 4 instructions are necessary for ptr = array + i; and only 2 instructions for ptr++; in this assembly language.

Basically when stepping though an array, you can avoid a multiply(or shift left) op that is need to multiply the index variable by the sizeof that type by simply adding the sizeof to a pointer each time. Incrementing a pointer in a loop is the only pointer arithmetic that I regularly use and the ptr can be even be used as the loop control variable eliminating i.

So basically I am saying (since my first post here) is pointer arithmetic (that can be done in C not Java) can be slightly more efficient in some situations. Nothing major that will make a significant difference in our robot code. Can we agree on that?

Quote:

Originally Posted by Alan Anderson (Post 860145)
My understanding is that they compile to exactly the same thing, since in C a[i] is just syntactical sugar for *(a+i). I used to confuse people by writing i[a] instead, which gives exactly the same result.

Exactly all array indexing is pointer arithmetic. However, not all pointer arithmetic is array indexing. More on that next


Quote:

Originally Posted by Alan Anderson (Post 860145)
Without significant context, I'm at a loss to understand why you'd want to write it. With the exception of using memory-mapped I/O, I have yet to see any examples anywhere where using absolute pointers gives any improvement in efficiency over using arrays.

I accidentally stuck my explanation in the next Chris quote, I fixed it, this is how it should have read
Quote:

Originally Posted by The Lucas (Post 860130)
Code:

ptr = 365;
Array indexing requires a symbol (base of array) and a type (size of element). When an integer can be cast to a pointer, pointer arithmetic can be done without an array.

As you said, memory map I/O is the only use I can think of for that expression. However, that wasn't even meant to be a practical example. It was just meant provide an simple example of pointer arithmetic that didn't have a matching array index expression to refute Chris' statement:

Quote:

Originally Posted by Chris27 (Post 860124)
Array indexing is just an encapsulation for pointer arithmetic as I explained earlier. Any pointer arithmetic expression will have a matching array index expression and both of these expressions will compile to the exact same instructions.


Quote:

Originally Posted by Jared341 (Post 860153)
Because if you don't use pointer arithmetic, you can't be a real programmer. :cool:

(I know that's not Brian's argument, but it is a good excuse to post one of my all time favorite programming urban legends)

Great story :cool:

EricVanWyk 18-05-2009 14:30

Re: **FIRST EMAIL**/Java and Orbit Balls
 
Quote:

Originally Posted by Bharat Nain (Post 860149)
More importantly, how much does it matter in our application? What difference will it make?

Bharat just won this thread.

An instruction cycle here or there makes zero difference in the face of the larger picture. If you want to have a truly useless argument, just remember that our new processor is pipelined AND has a double dispatch with multiple ALU elements.

So while you are all optimizing the instructions, I'll be working on what I want the code to do.


As a point of interest, I know someone who wrote his own graphical programming language that compiles to something that is on average twice as fast as C for embedded power and motion control applications.

Foster 18-05-2009 16:12

Re: **FIRST EMAIL**/Java and Orbit Balls *****JAVA DISCUSSION***
 
Quote:

An instruction cycle here or there makes zero difference in the face of the larger picture. If you want to have a truly useless argument, just remember that our new processor is pipelined AND has a double dispatch with multiple ALU elements.
Right. We now have CPU cycles and memory space to burn, so a cycle here and a cycle there does not make that much difference.

Quote:

So while you are all optimizing the instructions, I'll be working on what I want the code to do.
I get calls on a regular basis to "make it run faster." I always ask "Does it run correctly now?" since there is no reason to optimize a program that does not work.

Quote:

As a point of interest, I know someone who wrote his own graphical programming language that compiles to something that is on average twice as fast as C for embedded power and motion control applications.
There are two optimizations here. The custom language is focused to help solve motion control applications. C is a general purpose language, an electronic "hammer" so to speak.

The second is that there is most likely a set of libraries that he uses for the down and dirty interfaces. He optimized them to work with his target platform.

Speed in Java the last few years has come from:
  • Improved VM processing - the actual runtime environment of the system
  • Improved "code generation" by the compiler
  • Improved library functions. By far the biggest speed pickups has been in the optimization of data structures in the system. Sorted lists are about 150% faster due to algorithm improvements (no longer the bumble sort) which then reflects back into the application.
You saw the same things with C and the IFI controller. You could roll your own code to do all the functions, but most of us took Kevin Watson's or the WPI library. Those key functions had been optimized first for "they work" then for "make them fast."

If you optimize the algorithm you seldom have to bit fiddle to make it faster.

Uberbots 18-05-2009 23:46

Re: **FIRST EMAIL**/Java and Orbit Balls
 
Quote:

Originally Posted by Jared341 (Post 859640)
What is it that you want to do with pointers that Java's pass by reference system can't do exactly?

Before i answer this, wow what a discussion this simple pointer thing turned into.

Anyway, it just bothers me that java turns the idea of pointers from a more numerical approach to an object-oriented approach. I like knowing that the variable i am passing into a function is explicitly a pointer, not something determined by java. Its also bothersome that for primitive datatypes such as int's an entirely different datatype (Integer) is required if you want to pass it by reference into a function (i know there is a system to go around this but god knows how much overhead it incurs, along with the garbage collection of the implicit object).

I'm a C man. i need to know what is happening with my code at all times. It helps (me) with debugging and low-level optimizing a lot. :]

also: double/triple/quad/etc pointers.

Wetzel 19-05-2009 05:38

Re: **FIRST EMAIL**/Java and Orbit Balls *****JAVA DISCUSSION***
 
Our programming mentor is excited, because his AP classes use Java. It never hurts to give the school a really obvious reason why the time commitment is beneficial now.


Wetzel

GeorgeTheEng 19-05-2009 07:36

Re: **FIRST EMAIL**/Java and Orbit Balls
 
Quote:

Originally Posted by Uberbots (Post 860333)
I'm a C man. i need to know what is happening with my code at all times. It helps (me) with debugging and low-level optimizing a lot. :]

In my experience, when pointers are no longer part of the equation... There is a LOT less debugging that ever needs to be done. And the debugging that does need to be done is usually simplier. When code crashes there is a traceable route to it rather then having a line of code in a random unrelated location in a separate thread give unidentifiable errors in the code running at this moment.

Granted the rules with Java references can get odd at times as well.

Honestly, working on a team where the school has no programming courses at all, I welcome being able to work with the kids using an industry standard language that does not involve having to address pointers and pointer arithmetic. Labview is not bad, but I see it as a niche product. C++ and Java have a much wider user base.

Alan Anderson 19-05-2009 10:37

Re: **FIRST EMAIL**/Java and Orbit Balls *****JAVA DISCUSSION***
 
Quote:

Originally Posted by Uberbots (Post 860333)
I'm a C man. i need to know what is happening with my code at all times.

This is a truism. When you use C, particularly when you use pointers, you must have a constant deep understanding of exactly what your code is doing. A whole category of bugs is possible that only the programmer is capable of tracking down and dealing with.

When you don't have the option of using pointers, you can usually trust that the compiler is keeping track of things for you appropriately. Debugging is a lot easier when your tools can show you how things are going wrong.

Uberbots 19-05-2009 12:13

Re: **FIRST EMAIL**/Java and Orbit Balls *****JAVA DISCUSSION***
 
Quote:

Originally Posted by Alan Anderson (Post 860388)
When you use C, particularly when you use pointers, you must have a constant deep understanding of exactly what your code is doing.

And i do. So whats the problem?


All times are GMT -5. The time now is 10:59.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi