java 递归数组求和,Java:测试数组求和算法的效率

本文探讨了在Java中计算ArrayList元素之和的三种方法:迭代、递归和数组分割结合递归。通过输出步骤数来评估效率,但注意到递归数组分割方法虽然步骤多,在某些情况下可能与迭代法效率相当。建议考虑更全面的性能测试,如处理大量数据时的内存消耗和栈溢出风险。
摘要由CSDN通过智能技术生成

I am taking a Java course in university and my notes give me 3 methods for calculating the sum of an ArrayList. First using iteration, second using recursion, and third using array split combine with recursion.

My question is how do I test the efficiency of these algorithms? As it is, I think the number of steps it takes for the algorithm to compute the value is what tells you the efficiency of the algorithm.

My Code for the 3 algorithms:

import java.util.ArrayList;

public class ArraySumTester {

static int steps = 1;

public static void main(String[] args) {

ArrayList numList = new ArrayList();

numList.add(1);

numList.add(2);

numList.add(3);

numList.add(4);

numList.add(5);

System.out.println("------------------------------------------");

System.out.println("Recursive array sum = " + ArraySum(numList));

System.out.println("------------------------------------------");

steps = 1;

System.out.println("Iterative array sum = " + iterativeSum(numList));

System.out.println("------------------------------------------");

steps = 1;

System.out.println("Array sum using recursive array split : " + sumArraySplit(numList));

}

static int ArraySum(ArrayList list) {

return sumHelper(list, 0);

}

static int sumHelper(ArrayList list, int start) {

// System.out.println("Start : " + start);

System.out.println("Rescursive step : " + steps++);

if (start >= list.size())

return 0;

else

return list.get(start) + sumHelper(list, start + 1);

}

static int iterativeSum(ArrayList list) {

int sum = 0;

for (Integer item : list) {

System.out.println("Iterative step : " + steps++);

sum += item;

}

return sum;

}

static int sumArraySplit(ArrayList list) {

int start = 0;

int end = list.size();

int mid = (start + end) / 2;

System.out.println("Rescursive step : " + steps++);

//System.out.println("Start : " + start + ", End : " + end + ", Mid : " + mid);

//System.out.println(list);

if (list.size() <= 1)

return list.get(0);

else

return sumArraySplit(new ArrayList(list.subList(0, mid)))

+ sumArraySplit(new ArrayList(list.subList(mid,

end)));

}

}

Output:

------------------------------------------

Rescursive step : 1

Rescursive step : 2

Rescursive step : 3

Rescursive step : 4

Rescursive step : 5

Rescursive step : 6

Recursive array sum = 15

------------------------------------------

Iterative step : 1

Iterative step : 2

Iterative step : 3

Iterative step : 4

Iterative step : 5

Iterative array sum = 15

------------------------------------------

Rescursive step : 1

Rescursive step : 2

Rescursive step : 3

Rescursive step : 4

Rescursive step : 5

Rescursive step : 6

Rescursive step : 7

Rescursive step : 8

Rescursive step : 9

Array sum using recursive array split : 15

Now from the above output the recursive array split algorithm takes the most steps, however according to my notes, it is as efficient as the iterative algorithm. So which is incorrect my code or my notes?

解决方案

Do you just want to look at speed of execution? If so, you'll want to look at microbenchmarking:

How do I write a correct micro-benchmark in Java?

Essentially because of how the JVM and modern processors work, you won't get consistent results by running something a million times in a FOR loop and measuring the execution speed with a system timer (EDIT).

That said, "efficiency" can also mean other things like memory consumption. For instance, any recursive method runs a risk of a stack overflow, the issue this site is named after :) Try giving that ArrayList tens of thousands of elements and see what happens.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值