Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function algo1(a: number[]) {
- return a.reduce((acum, iter) => Math.max(acum, iter));
- }
- const infinite = 1 / 0;
- function algo2(a: number[]) {
- let max = -infinite;
- // tslint:disable-next-line:prefer-for-of
- for (let i = 0; i < a.length; i++) {
- max = max < a[i] ? a[i] : max;
- }
- return max;
- }
- const tamaño = 1000;
- const array = new Array(tamaño);
- for (let i = 0; i < tamaño; i++) {
- array[i] = i;
- }
- const VECES = 1000;
- let antes = +new Date();
- for (let i = 0; i < VECES; i++) {
- algo1(array);
- }
- let despues = +new Date();
- console.log("Algoritmo 1: ", (despues - antes) / (1000 * VECES));
- antes = +new Date();
- for (let i = 0; i < VECES; i++) {
- algo2(array);
- }
- despues = +new Date();
- console.log("Algoritmo 2: ", (despues - antes) / (1000 * VECES));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement