首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Java中,while和使用递归方法哪个更快?

在Java中,while和使用递归方法哪个更快?
EN

Stack Overflow用户
提问于 2012-12-19 23:55:02
回答 8查看 5.3K关注 0票数 18

我有以下包含这两种方法的示例class

Process.java:

代码语言:javascript
复制
public class Process {

    public Process() {
    }

    public static void countRecursive(int num) {
        System.out.println("countRecursive: " + num++);
        if (num <= 10) countRecursive(num);
        else return;
    }

    public static void countWhile(int num) {
        do System.out.println("countWhile: " + num++);
        while (num <= 10);
    }

}

主类:

代码语言:javascript
复制
public static void main(String[] args) {        

    Process.countRecursive(0);
    Process.countWhile(0);

}

输出:

代码语言:javascript
复制
countRecursive: 0
countRecursive: 1
countRecursive: 2
countRecursive: 3
countRecursive: 4
countRecursive: 5
countRecursive: 6
countRecursive: 7
countRecursive: 8
countRecursive: 9
countRecursive: 10

countWhile: 0
countWhile: 1
countWhile: 2
countWhile: 3
countWhile: 4
countWhile: 5
countWhile: 6
countWhile: 7
countWhile: 8
countWhile: 9
countWhile: 10

但我想知道推荐使用哪种“技术”以及原因。

提前谢谢。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13956158

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档