JavaScript 如何获取执行速度最快的函数在函数数组中的索引

JavaScript 如何获取执行速度最快的函数在函数数组中的索引

在这个示例中,我们将学习如何在JavaScript中获取执行速度最快的函数在函数数组中的索引。

示例:

Input: fun[] = [ hello, hello1, hello2 ]
Output: index of fastest is 0.

Explanation: Function hello execute fastest in all functions.

Input: fun[] = [ while1, while2, while3 ]
Output: index of fastest function is 2 

方法: 解决问题需要遵循以下步骤:

  • 首先,我们将遍历给定的数组。
  • 我们将找出每个函数的执行时间,并将其存储在一个不同的数组中,与函数索引具有相同的索引值。可以通过使用 performance.now() 方法来计算执行时间。
  • 最后,我们通过使用 Math.min() 方法来获取数组的最小值,打印出最小的索引。

示例1

在这个示例中,我们将计算每个函数执行所需的时间。然后,使用上述方法打印出最快函数的索引。

<script> 
    // 1st function 
    function hello() { 
      var s = ""; 
      var ans = ["The", " hello function ", "takes "]; 
      for (var i = 0; i < 3; i++) s += ans[i]; 
      console.log(s); 
    } 
      
    // 2nd function 
    function hello1() { 
      var s = ""; 
      var ans = ["The hello1 function", " takes "]; 
      for (var i = 0; i < 2; i++) s += ans[i]; 
      console.log(s); 
    } 
      
    // 3rd function 
    function hello2() { 
      var ans = "The hello2 function takes "; 
      for (var i = 0; i < 1; i++) console.log(ans); 
    } 
      
    // Function to check time required by each function 
    function findTime(f) { 
      
      // Storing initial time in start 
      var start = performance.now(); 
      
      // Calling the function 
      f(); 
      
      // Storing time after running the function 
      var end = performance.now(); 
      
      // Return time taken by function 
      return end - start; 
    } 
      
    function findMinTime() { 
      
      // Initializing array of functions 
      var fun = [hello, hello1, hello2]; 
      
      // Initialising array of time taken by function 
      var ans = []; 
      
      // Iterating over all the functions and 
      // storing time taken by them 
      for (var i = 0; i < 3; i++) { 
        var n = findTime(fun[i]); 
        ans[i] = n; 
        console.log(ans[i]); 
      } 
      
      // Finding the minimum time in array 
      var answer = Math.min.apply(null, ans); 
      c = ans.indexOf(answer); 
      
      // Return index of fastest array 
      return c; 
    } 
      
    var minTime = findMinTime(); 
    console.log("Index of fastest function:", minTime); 
</script>

输出:

"The hello function takes "
0.10000000009313226
"The hello1 function takes "
0
"The hello2 function takes "
0
"Index of fastest function:"
1

示例2

在此示例中,我们将计算函数执行某些数学运算所需的时间。然后,我们将打印出最快函数的索引。

<script> 
    // 1st function 
    function fac(n) { 
        let fact = 1; 
        for (let i = 1; i <= 4; i++) 
        fact *= i; 
        console.log("Factorial of 4 is:", fact); 
    } 
      
    // 2nd function 
    function fibo() { 
        let fab = 0; 
        let j = 1; 
        for (let i = 2; i <= 6; i++) { 
        let temp = fab; 
        fab += j; 
        j = temp; 
        } 
        console.log("6th fibonacci no is:", fab); 
    } 
      
    // 3rd function 
    function binpow() { 
        let j = 2; 
        let k = 22; 
        for (let i = 0; i < k; i++) 
        j = ((j * j) % 1e9) + 7; 
        console.log( 
        "Power 2 to 22 mod 1e9+7 is:", j 
        ); 
    } 
      
    // Function to check time required 
    // by each function 
    function findTime(f) { 
      
        // Storing initial time in start 
        var start = performance.now(); 
      
        // Calling the function 
        f(); 
      
        // Storing time after running the function 
        var end = performance.now(); 
      
        // Return time taken by function 
        return end - start; 
    } 
      
    function findMinTime() { 
      
        // Initializing array of functions 
        var fun = [fac, fibo, binpow]; 
      
        // Initialising array of time 
        // taken by function 
        var ans = []; 
      
        // Iterating over all the functions 
        // and storing time taken by them 
        for (var i = 0; i < 3; i++) { 
        var n = findTime(fun[i]); 
        ans[i] = n; 
        console.log(ans[i]); 
        } 
      
        // Finding the minimum time in array 
        var answer = Math.min.apply(null, ans); 
        c = ans.indexOf(answer); 
      
        // Return index of fastest array 
        return c; 
    } 
      
    var minTime = findMinTime(); 
    console.log("Index of fastest function:", 
                minTime); 
</script>

输出:

Factorial of 4 is: 24
0.30000001192092896
6th fibonacci no is: 5
0.20000001788139343
Power 2 to 22 mod 1e9+7 is: 221047735
0.30000001192092896
Index of fastest function: 1

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程