Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By pbrockway2
  • 1 Post By pbrockway2

Thread: Questions about if statements?

  1. #1
    smande3 is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default Questions about if statements?

    what would be optimum for an if statement comparison speed wise?

    the real expample im working on is a texas hold em poker simulator but for the simplicity of my point and to not miss advice from non poker players i will make a different example.

    In the real situation i have "Card" objects with integer instance variables represented by rank and suit to be compared to build a poker hand. but here is a simplified version that can get my point across of where my question lies

    say i have an arraylist of 4 integers that are choosen at random (can be between 1 and 10).

    the array list is sorted least to greatest into a new arraylist through a method (im already confident there is no issue in this method so i will not post)

    now say i want to test to see whether either of these are true from the top down

    4 of the same
    5 in order (example 2,3,4,5)
    3 of the same
    2 of the same and 2 of another (example 2,2,3,3)


    obviously im going to have to do some conditional testing here to be able to correctly rank my set of integers.

    heres where the questions begin

    rather than call arrayListInQuestion.get(i).getvalueOfIntegerForExa mple for every test would it be quicker before testing to initialize variables for them like say: i1= arrayListInQuestion.get(i).getvalueOfIntegerForExa mple
    than i can just call i1 every time rather than calling an arraylist object than calling a getter method on it.

    if im right on that i think i would make 4 integer variables in the testing method i1 for the arraylist zero index i2 for the 1 index and so on

    so in order for there to be four of a kind or three of a kind there must be a pair to test for a pair would it be dumb to have this many or statements

    if(i1==i2||i2==i3||i3==i4) {
    than test for three of a kind if three of a kind test for of a kind, also if there is a pair dont bother testing for a sequence cause it would be impossible

    else test for sequence... if(i2 ==i1+1 && i3==i1+2....

    see my logic is why test every set of elements for 4 of a kind and three of a kind if you can save tests by ruling out the possibility at the same time testing for a less specific/weaker holding, a pair with happen semi frequent but 4 of a kind is rare so dont test for for of a kind if you dont even have a pair, than in the game application if say player 1 has 3 of a kind and player 2 has a pair no need to know 3 of a what and a pair of what that would be more unessesary testing because 3 of a kind beats a pair so why bother wasting recources to figure the unessary information only specify when say two players hold the same general holding

    im mainly looking for help in proper if statement usage and the arraylist calling question

    is it slow to have many ands in or's in a conditional statement

    if in a more complex application if there is alot of code in a if statements body does it take more time to pass over it?

    is it bad to have nested if's within if's

    in which cases is it better to go to a whole new method than scan a long line of if's

    do you know of any articles/ resources on this subject?

    the question is very long winded but im a begginer javaist who is really becoming intrigued with it.

    sorry if i did a poor job explaining my question

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Questions about if statements?

    Prefer readable code over everything.

    If there might be a conflict between speed and readability, prefer readability. It might turn out that a change to slightly less readable code leads to a speed improvement. (this is the sort of thing that can *only* be established by testing and timing - don't imagine that you're cleverer than the compiler and can determine slow vs fast just by thinking about it) But you are in the best position to make such improvements if you are starting with readable code. Unreadable code will lead to a muddle: whatever speed claims are made on its behalf.

    (In passing we should note that the total number of comparisons involved in evaluating a poker hand is rather small as computer calculations go. The total amount of time you might possibly save is to be measured in nanoseconds. Don't bother. If you find your application "freezing", or taking any appreciable time, every time such a calculation is made then you have a definite problem that can and should be addressed.)

    So, what is readable code?

    A good test is that you should be able to post it here without non poker playing forum members passing over it muttering something impolite to the effect of "tldr;".

    The more variables you have in a method, the more variables the reader has to carry in their head while reading the code. So I would want to stay away from i1, i2 etc. Remember that there is an asList() method in the Arrays class that will return you the list elements in an array. Then they can be referred to as arr[0], arr[1], etc. If your list contains a complex type like cards and you want just the values to test for pairs etc, then a separate method could construct an array of card values.

    Nesting if blocks within if blocks etc, etc is not bad for any reason of speed. But it does detract from readability. Prefer using many small methods whose behaviour is precisely documented in the code. Likewise very long involved boolean conditions are hard to read: consider using a separate method whose job it is to calculate and return the boolean result.

    You are probably right in thinking that there are "shortcuts" like not looking for 4 of the same if there are no pairs. The exact algorithm will depend on exactly what you are trying to calculate. But you might find it most straight forward to fully evaluate a hand ie identify all pairs, three of a kind etc even if you end up with extra information when it comes to deciding between a hand with 3 of a kind and a hand with a pair. Such an evaluation would be independent of what hand was being compared with what and hence would be easier to write, to read and to test.
    cselic likes this.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Questions about if statements?

    I might add that you shouldn't become immobilised by uncertainty over what course of action is optimum.

    Dive in and write some code. Keep the methods small and test as you go (against behaviour that you document in the code). It might not be pretty to start with. Writing good code (as if I would know!) is a matter of practice rather than following by rote some rules or other.
    kjkrum likes this.

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Re: Questions about if statements?

    Indeed... don't worry about these kinds of optimizations unless the program's performance is inadequate. If you start thinking about these kinds of things prematurely, you'll get bogged down in the details and never finish the program.

    The main problem with nested 'if's is that the code quickly becomes unreadable. This is especially true if the logic doesn't all fit on one page. You suggested one possible solution to that: make the logic more readable by replacing the 'if' conditions with methods. For example, a bunch of code that determines if a hand is a full house could be condensed to one line with a method call like isFullHouse(Collection<Card> hand).
    Get in the habit of using standard Java naming conventions!

  5. #5
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default Re: Questions about if statements?

    @smande3:

    is it slow to have many ands in or's in a conditional statement
    No.

    if in a more complex application if there is alot of code in a if statements body does it take more time to pass over it?
    Yes it takes more time, but it's so small amount that almost there is no difference between if with a lot of code and if without a lot of code.

    Everything what you have mentioned here as it might be speed problem could not be a speed problem at all.

    Only thing that it could be serious speed problem is for what you are using ArrayList, or did you choose ArrayList as good Collection for your application. Is there maybe a better and faster Collection for your application? If you give to yourself answer to that question then your application should be more faster and that's the main question of speed improvement, instead of problem of lots of if statements and many "ands, ors,..." that are making my application slower.

    Little example:
    Inserting an element between existing elements of an ArrayList or Vector is an inefficient operation - all elements after the new one must be moved out of the way, which could be an expensive operation in a collection with a large number of elements.
    A LinkedList enables efficient insertion (or removal) of elements in the middle of a collection.

    do you know of any articles/ resources on this subject?
    A Beginner’s Guide to Big O Notation Rob Bell
    Last edited by cselic; 07-15-2012 at 10:37 AM.

Similar Threads

  1. If Statements
    By WillHorne in forum New To Java
    Replies: 7
    Last Post: 03-01-2012, 12:47 PM
  2. If Statements
    By katymccl in forum New To Java
    Replies: 6
    Last Post: 02-02-2012, 08:06 AM
  3. if else statements
    By sweetpea123 in forum New To Java
    Replies: 4
    Last Post: 04-12-2010, 07:02 PM
  4. age: using if statements
    By yasmin k in forum New To Java
    Replies: 2
    Last Post: 10-04-2009, 09:50 PM
  5. While and do while statements questions
    By hungdukie in forum New To Java
    Replies: 39
    Last Post: 12-10-2008, 02:08 PM

Posting Permissions

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