green-code-initiative/ecoCode-challenge

[Hackaton 2024][Rhodium-Antimoine][PYTHON] EC2 - AvoidMultipleIfElseStatement

Chmaqo opened this issue · 0 comments

Associated rule title

AvoidMultipleIfElseStatement : do not use multiple if, else if, else statements

Associated rule link
https://github.com/green-code-initiative/ecoCode/blob/main/ecocode-rules-specifications/src/main/rules/EC2/python/EC2.asciidoc

Language and platform

Language Python 3.10.12
Platform : Lenovo T570
CPU : Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz (x86_64)
RAM : 2133 MHz - 8GB

Measure justification

Using VJoule and 1000000 runs on two samples (a good one and a bad one), here are the results :

Processing rule EC2 - language python
RUNNING vjoule
TESTING good CASE

time    1s697ms87µs
PDU     0.00 J
CPU     13.48 J
RAM     0.96 J
GPU     0.24 J
----------------------
RUNNING vjoule
TESTING bad CASE

time    1s557ms165µs
PDU     0.00 J
CPU     12.59 J
RAM     0.74 J
GPU     0.15 J
----------------------

Validation

The measurement performed invalidates the rule for the pyhton language.

The percentage of certainty of this validation 70%, the results seem to vary from one run to the other, and in more than 50% of the runs the bad case performs better (less execution time, less CPU power, less RAM power, less GPU power) :

# GOOD PYTHON CASE
def main():
    good_key = "abc"
    value = None
    match good_key:
        case "PEHLIGQINW":
            value = "TITBWCROGZ"
        case "EODOBILLE_":
            value = "QWXALPNCUH"
        case "DFATSHCYQM":
            value = "TENICGGKFE"
        case "CJTOKPWXBY":
             value = "ROS_SVPVKR"
        case "IAVHTWITCC":
             value = "KGEQHUKCYA"
        case "G_GE_RIEHK":
             value = "EGNZMMXLMR"
        case "UMIWNMHRLF":
             value = "PLAHARAQGE"
        case "VTCEAGODFB":
             value = "DGUCVDMWD_"
        case "W_AJBBAM_O":
             value = "SKKJHSEWWF"
        case "TBAUIJ_CCF":
             value = "RGJDWBFUMK"
        case "FYQPBLPOYI":
             value = "ZFVCZGVBDM"
        case "FGIHP_EJDA":
             value = "VQIVGMWCTN"
        case "DUKTEQEJMM":
             value = "MXHNQDZ_FT"
        case "FMSFWGJUEL":
             value = "PSPJHJYUXO"
        case "IHPBGUHPYE":
             value = "IMMJJMZPSI"
        case "CJHRVVKE_O":
             value = "BEXZOCAL_O"
        case "TWQJINCFRX":
             value = "ADEIQRGT_S"
        case "ZNJARYYUOQ":
             value = "ZSLBYEBPAS"
        case "IXYUPWDZHN":
             value = "XKCJZTDGQM"
        case "UQAIRGSBFC":
             value = "UGNHIKGSJA"
        case "DCRYAIRCIW":
             value = "FRTEBMTSQE"
        case "VLROVSZIAJ":
             value = "DXTTW_JECR"
        case "DFJMSSQCWY":
             value = "ERQCRRHLPS"
        case "VQTETFGVCU":
             value = "QYISZSZVYG"
        case "XRGGHPUKNM":
             value = "HOCW_M_PDL"
        case "HOINZCJAUN":
             value = "RIRR_SBQFL"
        case "FEJLPRTFDZ":
             value = "ZPBLEJSENI"
        case "AKRKVLPOFR":
             value = "NBACYUHBJU"
        case "HQUYRQZZHN":
             value = "ZCRIDPYCCN"
        case "FPWAFHUMXG":
             value = "V__WDYJVGW"
        case "YTH_MTSVSY":
             value = "YML_LYZJEN"
        case "LALJEZMFZW":
             value = "URAFUBXVFS"
        case "BDBWRELPIO":
             value = "JLPWMXDAZQ"
        case "GTGVYTBRLP":
             value = "UKGJHWMZSH"
        case "UGPQDIZRBK":
             value = "TUHPJJIRPP"
        case "ATXYDLXNNR":
             value = "UXNHBLOL_A"
        case "JPADWEKXXO":
             value = "SATWZTE_AY"
        case "UCOZSMASEA":
             value = "QWOGE_YQBD"
        case "BZCIJCS_NB":
             value = "NJDXTUFBQS"
        case "IRGLPTAK_Q":
             value = "IZEZSFSPZR"
        case "EJMBIAYQLD":
             value = "LSICC_JHZE"
        case "PILGTHVTLF":
             value = "UNYMROFGQF"
        case "QNSUHNEZKV":
             value = "NAZM_SZKFT"
        case "FUAVOTEGOC":
             value = "GBAQBCMDBM"
        case "VS_EKHHXSJ":
             value = "ECGHFFPGLJ"
        case "LURALFSTB_":
             value = "WEDEZKNOET"
        case "ITSEBKBRSP":
             value = "A_C_QYFVSO"
        case "WYGCTOCJZL":
             value = "LJTXWSRPSJ"
        case "QAGCUTERPP":
             value = "JDYXCUZAHU"
        case "GDTGAAIBLJ":
             value = "DQI_NCRWCG"
        case "abc":
             value = "def"
    return value

if __name__ == "__main__":
    for i in range(1000000):
        main()
# BAD PYTHON CASE
def main():
    test_value = "abc"
    result_value = None
    if test_value == "PEHLIGQINW":
        result_value = "TITBWCROGZ"
    elif test_value == "EODOBILLE_":
        result_value = "QWXALPNCUH"
    elif test_value == "DFATSHCYQM":
        result_value = "TENICGGKFE"
    elif test_value == "CJTOKPWXBY":
        result_value = "ROS_SVPVKR"
    elif test_value == "IAVHTWITCC":
        result_value = "KGEQHUKCYA"
    elif test_value == "G_GE_RIEHK":
        result_value = "EGNZMMXLMR"
    elif test_value == "UMIWNMHRLF":
        result_value = "PLAHARAQGE"
    elif test_value == "VTCEAGODFB":
        result_value = "DGUCVDMWD_"
    elif test_value == "W_AJBBAM_O":
	    result_value = "SKKJHSEWWF"
    elif test_value == "TBAUIJ_CCF":
	    result_value = "RGJDWBFUMK"
    elif test_value == "FYQPBLPOYI":
	    result_value = "ZFVCZGVBDM"
    elif test_value == "FGIHP_EJDA":
	    result_value = "VQIVGMWCTN"
    elif test_value == "DUKTEQEJMM":
	    result_value = "MXHNQDZ_FT"
    elif test_value == "FMSFWGJUEL":
	    result_value = "PSPJHJYUXO"
    elif test_value == "IHPBGUHPYE":
	    result_value = "IMMJJMZPSI"
    elif test_value == "CJHRVVKE_O":
	    result_value = "BEXZOCAL_O"
    elif test_value == "TWQJINCFRX":
	    result_value = "ADEIQRGT_S"
    elif test_value == "ZNJARYYUOQ":
	    result_value = "ZSLBYEBPAS"
    elif test_value == "IXYUPWDZHN":
	    result_value = "XKCJZTDGQM"
    elif test_value == "UQAIRGSBFC":
	    result_value = "UGNHIKGSJA"
    elif test_value == "DCRYAIRCIW":
	    result_value = "FRTEBMTSQE"
    elif test_value == "VLROVSZIAJ":
	    result_value = "DXTTW_JECR"
    elif test_value == "DFJMSSQCWY":
	    result_value = "ERQCRRHLPS"
    elif test_value == "VQTETFGVCU":
	    result_value = "QYISZSZVYG"
    elif test_value == "XRGGHPUKNM":
	    result_value = "HOCW_M_PDL"
    elif test_value == "HOINZCJAUN":
	    result_value = "RIRR_SBQFL"
    elif test_value == "FEJLPRTFDZ":
	    result_value = "ZPBLEJSENI"
    elif test_value == "AKRKVLPOFR":
	    result_value = "NBACYUHBJU"
    elif test_value == "HQUYRQZZHN":
	    result_value = "ZCRIDPYCCN"
    elif test_value == "FPWAFHUMXG":
	    result_value = "V__WDYJVGW"
    elif test_value == "YTH_MTSVSY":
	    result_value = "YML_LYZJEN"
    elif test_value == "LALJEZMFZW":
	    result_value = "URAFUBXVFS"
    elif test_value == "BDBWRELPIO":
	    result_value = "JLPWMXDAZQ"
    elif test_value == "GTGVYTBRLP":
	    result_value = "UKGJHWMZSH"
    elif test_value == "UGPQDIZRBK":
	    result_value = "TUHPJJIRPP"
    elif test_value == "ATXYDLXNNR":
	    result_value = "UXNHBLOL_A"
    elif test_value == "JPADWEKXXO":
	    result_value = "SATWZTE_AY"
    elif test_value == "UCOZSMASEA":
	    result_value = "QWOGE_YQBD"
    elif test_value == "BZCIJCS_NB":
	    result_value = "NJDXTUFBQS"
    elif test_value == "IRGLPTAK_Q":
	    result_value = "IZEZSFSPZR"
    elif test_value == "EJMBIAYQLD":
	    result_value = "LSICC_JHZE"
    elif test_value == "PILGTHVTLF":
	    result_value = "UNYMROFGQF"
    elif test_value == "QNSUHNEZKV":
	    result_value = "NAZM_SZKFT"
    elif test_value == "FUAVOTEGOC":
	    result_value = "GBAQBCMDBM"
    elif test_value == "VS_EKHHXSJ":
	    result_value = "ECGHFFPGLJ"
    elif test_value == "LURALFSTB_":
	    result_value = "WEDEZKNOET"
    elif test_value == "ITSEBKBRSP":
	    result_value = "A_C_QYFVSO"
    elif test_value == "WYGCTOCJZL":
	    result_value = "LJTXWSRPSJ"
    elif test_value == "QAGCUTERPP":
	    result_value = "JDYXCUZAHU"
    elif test_value == "GDTGAAIBLJ":
	    result_value = "DQI_NCRWCG"
    elif test_value == "abc":
	    result_value = "def"

if __name__ == "__main__":
    for i in range(1000000):
        main()