Results 1 to 16 of 16
  1. #1
    marcosol is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default 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!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default 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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JeffThomas is offline Member
    Join Date
    Dec 2011
    Posts
    31
    Rep Power
    0

    Default 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.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default 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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    marcosol is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default 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.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default 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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    JeffThomas is offline Member
    Join Date
    Dec 2011
    Posts
    31
    Rep Power
    0

    Default 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.

  8. #8
    JeffThomas is offline Member
    Join Date
    Dec 2011
    Posts
    31
    Rep Power
    0

    Default 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.

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default 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."
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    JeffThomas is offline Member
    Join Date
    Dec 2011
    Posts
    31
    Rep Power
    0

    Default 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.

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default 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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    JeffThomas is offline Member
    Join Date
    Dec 2011
    Posts
    31
    Rep Power
    0

    Default 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.

  13. #13
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default 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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: efficiency problem with homework

    @JeffThomas: please stop your personal attacks; that was uncalled for.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: efficiency problem with homework

    Quote Originally Posted by JeffThomas View Post
    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?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    sterf is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: efficiency problem with homework

    I learned always to do as follows, measure instead of guessing. Guessing takes too much time.

    Java Code:
    long start = System.currentTimeMillis();
    doStuff();
    Java Code:
    long delta = System.currentTimeMillis()-start;
    System.out.println("took " + delta+ "ms");

Similar Threads

  1. Truth Table Homework Problem
    By HardToHandle in forum New To Java
    Replies: 0
    Last Post: 02-13-2012, 06:46 AM
  2. Letter counter homework problem
    By Djgnl in forum New To Java
    Replies: 15
    Last Post: 09-24-2011, 03:37 AM
  3. Help with Homework Problem
    By Holy_Zombies in forum New To Java
    Replies: 6
    Last Post: 10-05-2010, 07:26 AM
  4. Problem using thread +rmi in my homework
    By IbrahimAbbas in forum Threads and Synchronization
    Replies: 10
    Last Post: 04-14-2008, 10:24 PM
  5. Homework PREREQUISITE Problem
    By ChrisC in forum New To Java
    Replies: 7
    Last Post: 11-27-2007, 06:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •