0

I have a thesis going on regarding comparisons between Swift and React-Native. For the performance of both languages i compare basic algorithms such as Sieve and recursive fibonacci.

I have implemented recursive fibonacci in a react project and now I want to time the algorithm's execution time.

Here is my code so far:

const App: () => React$Node = () => {

  var num = 20;
  let time1;
  let time2;
  let executionTime;
  var fibNum;

  return (
    <View style ={{flex: 1,
      justifyContent: "center",
      alignItems: "center",
      margin: 8,
      }}>


    <Text style = {{fontSize: 24}}> {fib_run(num)} </Text>
    <Text style = {{fontSize: 18}}>Time it took was {executionTime} ms</Text>

    </View>
  );


  function fibonacci_rec(num){

    if(num <= 1)
    return 1;

    return fibonacci_rec(num - 1) + fibonacci_rec(num - 2);
  }


  function fib_run(num){

    time1 = new Date().getMilliseconds();
    fibNum = fibonacci_rec(num);
    time2 = new Date().getMilliseconds();
    executionTime = (time2 - time1);

    return fibNum;
  }


};

For some reason the executionTime variable always shows 0. How do I solve this?

(I have very little knowledge in React-Native)

Kind regards, Stefan

1 Answers1

0

And what's the problem? It seems to have taken 0 ms. If that's not acceptable, try incrementing num. I got Time it took was 5 ms for num = 28.

windowsill
  • 3,599
  • 1
  • 9
  • 14