Hi, bytecode and control flow enthusiasts. This is a lengthy post, but it’s primarily on me wanting to know if I went wrong in any way in my benchmarking, as well as me wanting to know how a developer could find the most optimal design for logically equivalent conditional expressions.
That brings my question: In what way does cyclomatic complexity (with additionally considering the order of execution), as reflected by the bytecode instructions of a section of code, affect its runtime? I’m specifically analyzing 5 pieces of code that represent the same, logically & functionally equivalent functions:
- A logical expression in (an unsimplified) disjunctive normal form (DNF, also known as SOP for sum of products),
- and its simplest form–both found and translated from the Karnaugh map - Wikipedia page.
- And, I’ve included its conjunctive normal form (CNF, also known as POS for product of sums) of the expression.
The first three functions, in Luau:
The last two are written as if-then-else blocks in Luau, but incorporate logical implications where I’ve arbitrarily chosen work. For the last two function implementations, you have to trust that they are logically equivalent to the Boolean I/O above:
If you want to verify this for yourself, you can check out and test the script I’ve written to do exactly that:
And, this should output if they truly represent equivalent statements:

I’m using boatbomber’s Benchmarker Plugin to compare the execution time of each of the 5 functions, over 1000 iterations each. For each function’s iteration, I pass along the tuples of a table “booleans” containing the 2^4 possible (a, b, c, d) tuple combinations, as tables.
Note that I have allowed for reversing the direction of the initial table.
For each iteration, I attempt to randomly rearrange the list to a combinatorially full, but randomly ordered, list of the 2^4 states of (a, b, c, d). For this, I decided to try and implement the Fisher-Yates shuffle. Please tell me if you can find any mistakes in the probabilistic logic here:
Here’s the code that runs in the benchmarker:
I set the seed for rebuild to 0, but kept functionality for consistent debugging. Tell me if I also went wrong with the scope of randomness here.
For the test results, here you go:
randomized_order = true
reversed_table = true
reversed_table = false
randomized_order = false
reversed_table = true
reversed_table = false
I can’t find any patterns between altering reversal and then randomization, other than that reversing my original table consistently reduces the variances of each of the conditional_1 and simplified_dnf distributions.
What remains consistent among all conditions is that the tier list of the fastest functions goes:
conditional_1conditional_2simplified_dnfsimplified_cnffull_dnf
Here are the functions in the order I wrote in the code:
local full_dnf = function(a, b, c, d) -- 43, 31, 8
return (not a and b and c and not d)
or (a and not b and not c and not d)
or (a and not b and not c and d)
or (a and not b and c and not d)
or (a and not b and c and d)
or (a and b and not c and not d)
or (a and b and not c and d)
or (a and b and c and not d)
end
local simplified_dnf = function(a, b, c, d) -- 12, 6, 3
return (a and not c) or (a and not b) or (b and c and not d)
end
local simplified_cnf = function(a, b, c, d) -- 12, 6, 3
return (a or b) and (a or c) and (not b or not c or not d)
end
local conditional_1 = function(a, b, c, d) -- 6, 2, 2
if b then
if c then
return not d
else
return a
end
else
return a
end
end
local conditional_2 = function(a, b, c, d) -- 19, 9, 5
if a then
if (not c) or (not b) then
return true
elseif b and c and not d then
return true
else
return false
end
elseif b and c and not d then
return true
else
return false
end
end
Reading the bytecode for each function in the order presented above (check here because it’ll clog up this post), you can count the corresponding # of instructions, # of jump instructions, # of jump destinations–I’ve denoted these as comments on the same lines as the function initiation.
With the number of bytecode instructions not perfectly linearly correlated with the runtime order, I decided that runtime is nontrivially affected by control flow mechanisms such as an early return or short-circuiting, in order to alter the # or density of JUMPIF/JUMPIFNOT instructions. I can’t wrap my head around a clear pattern, and in what ways could density be refactored for performance? In the zenith of code highly optimized for runtime behavior, how would one go about using knowledge of the most likely true/false values for each input variable to create the most optimal ordering of conditional expressions?
All I know right now is
- (1) that the bytecode of logically equivalent conditionals CAN be optimized,
- (2) it seems more optimal to order most likely returnable conditionals ahead of other conditionals to take advantage of breaking early, and
- (3) to write conditionals in the most terse, but functionally complete set of logical operations, as (=>, NOT) shows to be consistently optimal in my example, though I need to find more logically equivalent statements to compare. But right now, it’s actually too hard for my pea brain to find more logically equivalent but unsimplified expansions, and I can’t find a way to do this online, both deterministically and accurately.
In the end, I couldn’t find a clear pattern in how to algorithmically approximate/order the runtime of these conditional-rooted functions, given their bytecode breakdown. I’m a beginner at analyzing bytecode and an even more immature learner of control flow.
Here’s the code I used for the benchmarker:
Any experiential knowledge or advice on where I can learn more about this theory would be helpful. Thanks. And as always, please correct me if I went wrong anywhere.










