Which way is faster? If elseif or select case

前端 未结 6 1298
陌清茗
陌清茗 2020-12-20 14:13

For the following code,

If Sheets(\"sheet1\").Range(\"A1\").Value = \"option_1\" Then
    Sheets(\"sheet1\").Range(\"A1\").Value = \"option_2\"
ElseIf Sheets         


        
6条回答
  •  被撕碎了的回忆
    2020-12-20 14:44

    I ran multiple scenarios comparing the "IF" statement with the "Case" statement using a simple "for loop" and "mod" function. The scenarios vary in the number of records checked and the number of conditions (# of ifElse/# of cases). The table below shows the scenarios and the results for each scenario. Note that the speed mentioned in the table is the average of 100 runs.

    We can see that the "IF" statement and "Case" statement are almost similar in performance. The "IF" statement slightly beats the "Case" statement (~5% faster).

    Excel Version: Office 365 version 1908 build 11929.20300

    Processor Used: Intel i9-9900K

提交回复
热议问题