当前位置:网站首页>Scala语言学习-05-递归和尾递归效率对比
Scala语言学习-05-递归和尾递归效率对比
2022-06-22 14:28:00 【阳光九叶草LXGZXJ】
一、测试环境
| 名称 | 版本 |
|---|---|
| 操作系统 | win10 |
| CPU | 12th Gen Intel Core i7-12700H |
| 内存 | 16G |
| JDK | 1.8.0_171 |
| Scala | 3.1.2 |
二、代码
import java.util.Date
object TestRecursion {
def main(args: Array[String]): Unit = {
val TempNum: Long = 12L
ComputeProcedureTime(Factorial, TempNum,"递归:")
ComputeProcedureTime(TailFactorial, TempNum,"尾递归:")
}
def ComputeProcedureTime(Func :Long => Long, Param: Long, Info: String): Unit = {
println("++++++++++++++++++++++++++++")
println(Info)
var start_time =new Date().getTime
println(Func(Param))
var end_time =new Date().getTime
println(s"Elapsed Time : ${
end_time-start_time} ms")
}
def Factorial(Num: Long): Long = {
if (Num == 1){
Num
}
else {
Num * Factorial(Num - 1)
}
}
def TailFactorial(Num: Long): Long = {
def Loop(Num: Long,Res: Long): Long = {
if(Num == 1){
Res
}
else{
Loop(Num - 1, Res * Num)
}
}
Loop(Num, 1)
}
}
三、运行结果
++++++++++++++++++++++++++++
递归:
479001600
Elapsed Time : 1 ms
++++++++++++++++++++++++++++
尾递归:
479001600
Elapsed Time : 0 ms

Idea会把递归和尾递归分别标识出来,点个赞
边栏推荐
- ROS2前置基础教程 | 使用CMakeLists.txt编译ROS2节点
- No wonder the postgraduate entrance examination is so hot. These are the "hidden benefits" of Postgraduates!
- 数据库连接池:压力测试
- 还整成这样
- Yilian technology rushes to Shenzhen Stock Exchange: annual revenue of RMB 1.4 billion, 65% of which comes from Ningde times
- 山东泰安“6·21”燃气爆炸事故后续:全面排查整治餐饮场所燃气安全隐患
- Simulation Keil et vspd
- Fast and accurate point cloud registration based on minimizing 3D NDT distance
- RealNetworks vs. Microsoft: the battle in the early streaming media industry
- All famous network platforms in the world
猜你喜欢

多年亿级流量下的高并发经验总结,都毫无保留地写在了这本书中

How MySQL modifies a field to not null

Once, I had 5 part-time jobs just to buy a new earring for my girlfriend

Database connection pool: implementation of connection pool function point

Show me my personal work list for the past two years. I earn 6K a month in my spare time. It's so delicious to have a sideline

个人免签支付方案推荐

Using virtual serial port to debug serial port in keil MDK

What happened to those who didn't go to college

建议自查!MySQL驱动Bug引发的事务不回滚问题,也许你正面临该风险!

口令安全是什么意思?等保2.0政策中口令安全标准条款有哪些?
随机推荐
The summary of high concurrency experience under the billion level traffic for many years is written in this book without reservation
宏源期货开户安全么?宏源期货公司可以降低手续费?
山东泰安“6·21”燃气爆炸事故后续:全面排查整治餐饮场所燃气安全隐患
Mobile learning notes of u++ programming
新版负载均衡WebClient CRUD
Sdvo:ldso+ semantics, direct French Slam (RAL 2022)
js中const定义变量及for-of和for-in
Is pioneer futures reliable? How to open a futures account safely?
Those confusing user state & kernel state
“软件定义世界,开源共筑未来” 2022开放原子全球开源峰会7月底即将开启
ML笔记-matrix fundamental, Gradient Descent
Using virtual serial port to debug serial port in keil MDK
还整成这样
【newman】postman生成漂亮的测试报告
Database connection pool: implementation of connection pool function point
大会倒计时 | 亚马逊云科技创新大会邀您一起构建AI新引擎 !
Sword finger offer46 -- translate numbers into strings
mysql如何将字段修改为not null
SDVO:LDSO+语义,直接法语义SLAM(RAL 2022)
How to use the concat() function of MySQL