efficiency problem with homework
I have a robot with a certain positon(x,y)and orietation(0,1,2,3) representing up,right,down,left.
My robot must be able to move one step forward condsidering his current orientation.
My solution looks like this
public void move ()
{
if(orientation==0)
setY(y-1);
if(orientation==1)
setX(x+1);
if(orientation==2)
setY(y+1);
if(orientation==3)
setX(x-1);
}
(y axis is pointing downwards)
Does anyone know a more efficient way to do this? If I should implement, for example, 4 diagonal directions. This would get messy..
(the orientations need to be a simple int, because of the GUI I need to use)
thanks!
Re: efficiency problem with homework
Define efficiency. Number of comparisons? Amount of code? Maintainability of code?
The best answer I can give you is this: does it work? Then don't worry about it. Premature optimization is the root of all evil.
You could try using a switch statement I suppose, or else if statements instead of just if statements. But like I said, you probably shouldn't worry about it too much.
Re: efficiency problem with homework
Are you required to calculate the time complexity of the algorithm?
Also, I was wondering if it was required to have the robot be able to move diagonally as well.
Re: efficiency problem with homework
Also, always use {curly brackets} with if statements, for loops, etc. Even if it's only one line, it will improve the maintainability and readability of your code.
Re: efficiency problem with homework
those are both not required. Maybe I should not worry that much. But Everyone in my classgroup will come by this solution and I wan to do better. So I guess I was looking for premature optimalisation aka the root of all evil:D:.
Re: efficiency problem with homework
The thing is, with code like this, optimization is rather pointless. The compiler is going to take your code and optimize it anyway, so trying to be smarter than the compiler is actually counter-productive. I would suggest using a switch or a series of if/else if statements instead of separate if statements. Your orientation variable is only going to equal one of those values, right? So why check all of them?
But this point, you're much better off spending your time focusing on improvements like adding the curly brackets and proper indentation. Improvements like that are what's going to set you apart from your classmates.
Re: efficiency problem with homework
Modern compilers optimize your code only to a certain extent. It's not like he is using assembly to try and beat the compiler.
In the end, it depends on the actual programmer to develop the right algorithm for the right task.
Just go ahead and try to use a recursive algorithm for the fibbionaci sequence o_O.
The if-else statements would be best here.
Re: efficiency problem with homework
The recursive algorithm with the Fibonacci sequence is an impossible task for the compiler. It takes almost infinite time to compute recursively.
That was just a trick to see if you had any clue what you thought were talking about.
Apparently, you don't.
Re: efficiency problem with homework
Sigh. I really don't feel like arguing about this. I'll just let you read for yourself: java - Why does the JVM still not support tail-call optimization? - Stack Overflow
"But although it is well known how to automatically transform a tail-recursive function into a simple loop, the Java specification doesn't require that this transformation be made. Presumably, one reason it is not a requirement is that, in general, the transformation can't be made statically in an object-oriented language. Instead, the transformation from tail-recursive function to simple loop must be done dynamically by a JIT compiler."
Re: efficiency problem with homework
This doesn't just have to do with java, it has to do with all programming in general. You should have learned this as an introductory topic, but you might have had your head in the clouds that day too.
Link:
CSci 160 Session 29: Recursion, Fibonacci numbers
You should know this already. I hope you take your time to learn it cause I'm scared for you.
The link should be helpful for all other people reading this as well.
Re: efficiency problem with homework
We're either talking about two different things, or you're misunderstanding what I'm saying. I understand recursion, and I've simply pointed out a way that a compiler may or may not optimize it for you. Either way, it's not pertinent to the OP's question, and personal attacks will not be tolerated. I'm fine with being called out when I'm wrong (but here, I don't think I've said anything incorrect), but please do so in a mature way. Consider this a warning.
Re: efficiency problem with homework
Backwards moderating isn't tolerated here either. If I get warnings for pointing out that your wrong, then come at meh.
Also, stop double posting on peoples threads. You don't need to post two times in a row just to make yourself seem more important.
Re: efficiency problem with homework
Like I said, I'm not going to argue with you, because I don't think you're talking about the same thing as me. You're talking about basic recursion. I'm talking about compiler optimization, including the possible optimization of recursion. Both of these things have little to do with the OP's original question. And you're making your arguments (which, again, I don't even think are related to anything I've said) by making immature attacks like "I'm scared for you" and "you had your head in the clouds". I don't really mind because I don't think you're talking about the same thing as me, but it doesn't really have a place on a technical forum.
If you want to continue this conversation, create your own thread or private message me.
Re: efficiency problem with homework
@JeffThomas: please stop your personal attacks; that was uncalled for.
kind regards,
Jos
Re: efficiency problem with homework
Quote:
Originally Posted by
JeffThomas
Also, stop double posting on peoples threads. You don't need to post two times in a row just to make yourself seem more important.
...what are you talking about?
Re: efficiency problem with homework
I learned always to do as follows, measure instead of guessing. Guessing takes too much time.
Code:
long start = System.currentTimeMillis();
doStuff();
Code:
long delta = System.currentTimeMillis()-start;
System.out.println("took " + delta+ "ms");