Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bd2720/ifswitch


https://github.com/bd2720/ifswitch

Last synced: about 1 month ago
JSON representation

Awesome Lists containing this project

README

        

Tests the efficacy of if-else statements vs. switch/case statements when
each branch has the same likelihood of occurring. if/else statements are
O(N), while switch should be O(1). So switch should be faster than if/else
at some value of N.

COMPILATION:
$ make

RUN:
$ ./ifSwitch

DESCRIPTION:

ifSwitch.h: defines the following adjustable constants:
ITERS : number of loop iterations for each test
N : number of equally-likely if-branches or switch-cases.

if: generates the if macro, "if.out"

if.out: if/else macro with N cases, generated by "if"

switch: generates the switch macro, "switch.out"

switch.out: switch/default macro with N cases, generated by "switch"

ifSwitch: runs and times each macro, "if.out" and "switch.out"

OBSERVATIONS:
- For 2 <= N < 45, IF beats SWITCH by as much as 1.2x.
- For N ~ 45, IF and SWITCH run at about the same speed.
- For N > 45, IF is slower than SWITCH:
- For N ~ 195, SWITCH beats IF by 2x.
- For N ~ 1100, SWITCH beats IF by 10x.
- For IF and large N, (N * 2) runs for twice as long as (N).
- For SWITCH, (N * 10) will take 15% longer to run than (N).