Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Nested conditionals vs oneline

in Questions Posts: 181

Hi all, so I know there is a performance difference between normal loops and unrolled loops and I was wondering if this is also the case with conditional statements. For example many nested if statements vs one big line with AND and ORs, assuming the conditional logic is identical?

For example

If a and b and c then ... end

vs

if a then
   if b then
      if c then
         ...
      end
   end
end

After reading wikipedia about loop unrolling I'm guessing no, but still curious.

Comments

  • I do not believe so BUT don't take my word for it

  • dave1707dave1707 Mod
    edited December 2016 Posts: 7,470

    @Kirl Here's a 10 times loop of 10 million iteration of each of your conditional statements. I print out the time it takes for the 10 million iterations for each of the 10 loops. They're times are close enough that it probable doesn't matter which one you use.

    EDIT: It's possible that when this gets converted to byte code, they turn out to be about the same code.

    function setup()
        a=true
        b=true
        c=true
        for g=1,10 do
            s=os.clock()
            for z=1,10000000 do
                if a and b and c then
                    x=1
                end
            end
            print(os.clock()-s)
    
            s=os.clock()
            for z=1,10000000 do
                if a then
                    if b then
                        if c then
                            x=1
                        end
                    end
                end
            end
            print(os.clock()-s)
            print()
        end
    end
    
  • edited December 2016 Posts: 181

    Haha, thanks dave, I've expanded your example to 10 conditionals, I guess there is a difference but like you said it probably doesn't matter in practice. I don't think it results in the same bytecode though, because there is a definite speed difference. The oneline conditional check is slightly faster.

    Tempting to take it to 100 conditionals... =)

    function setup()
        a=true
        b=true
        c=true
        d=true
        e=true
        f=true
        g=true
        h=true
        i=true
        j=true
        k=true
    
        for y=1,10 do
            s=os.clock()
            for z=1,10000000 do
                if a and b and c and d and e and f and g and h and i and j and k then
                    x=1
                end
            end
            print(os.clock()-s)
    
            s=os.clock()
            for z=1,10000000 do
                if a then
                    if b then
                        if c then
                            if d then
                                if e then
                                    if f then
                                        if g then
                                            if h then
                                                if i then
                                                    if j then
                                                        if k then
                                                            x=1
                                                        end
                                                    end
                                                end
                                            end
                                        end
                                    end
                                end
                            end
                        end
                    end
                end
            end
            print(os.clock()-s)
            print()
        end
    end
    
  • dave1707dave1707 Mod
    Posts: 7,470

    @Kirl I'm not sure how accurate the times are. I noticed when I ran your code, the pairs of time increased in time as they ran.

  • dave1707dave1707 Mod
    Posts: 7,470

    @Kirl Here's your above code, but using a different timer. This timer seems to be more accurate.

    function setup()
        sc=require("socket")
    
        a=true
        b=true
        c=true
        d=true
        e=true
        f=true
        g=true
        h=true
        i=true
        j=true
        k=true
    
        for y=1,10 do
            s=sc.gettime()
            for z=1,10000000 do
                if a and b and c and d and e and f and g and h and i and j and k then
                    x=1
                end
            end
            print(sc.gettime()-s)
    
            s=sc.gettime()
            for z=1,10000000 do
                if a then
                    if b then
                        if c then
                            if d then
                                if e then
                                    if f then
                                        if g then
                                            if h then
                                                if i then
                                                    if j then
                                                        if k then
                                                            x=1
                                                        end
                                                    end
                                                end
                                            end
                                        end
                                    end
                                end
                            end
                        end
                    end
                end
            end
            print(sc.gettime()-s)
    
            print()
        end
    end
    
  • dave1707dave1707 Mod
    Posts: 7,470

    @Kirl I compared the bytecodes created for the 2 sets of if statements below and the bytecodes for each were exactly the same. The difference in times for the above programs comparing the 2 loops was probably just other things running in the Codea code.

    ----------
        if a then
            if b then
                if c then
                    c=5
                end
            end
        end
    ----------
        if a and b and c then
            c=5
        end
    ----------
    
  • Posts: 181

    Huh, interesting, thanks!

    How did you convert to bytecode btw?

  • dave1707dave1707 Mod
    Posts: 7,470

    string.dump and a whole lot of work.

Sign In or Register to comment.