当前位置:网站首页>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会把递归和尾递归分别标识出来,点个赞
边栏推荐
- Driving the efficient growth of the manufacturing industry, UFIDA u9 cloud is constantly improving the password behind it
- Reconstruction practice of complex C-end project of acquisition technology
- Mitsubishi manipulator demo program
- 网络安全的五大特点有哪些?五大属性是什么?
- 历时100天、小鱼搭建了个机器人交流社区!!现公开邀请版主中!
- 天安科技IPO被终止:曾拟募资3.5亿 复星与九鼎是股东
- On the routing tree of gin
- Exploration and practice of dewu app data simulation platform
- 数据资产管理:数据发现,发现什么,怎么发现?
- Sword finger offer46 -- translate numbers into strings
猜你喜欢

封装api时候token的处理

Charles 乱码问题解决

Those confusing user state & kernel state

Recommendation of individual visa free payment scheme

Development status of full color LED display

How to use the concat() function of MySQL

【newman】postman生成漂亮的测试报告

Hongshi electric appliance rushes to the Growth Enterprise Market: the annual revenue is 600million yuan. Liujinxian's equity was frozen by Guangde small loan

Is the encryption market a "natural disaster" or a "man-made disaster" in the cold winter?

Yilian technology rushes to Shenzhen Stock Exchange: annual revenue of RMB 1.4 billion, 65% of which comes from Ningde times
随机推荐
再次认识 WebAssembly
架构师之路,从「存储选型」起步
Ask if you want to get the start of sqlserver_ Is there a good way for LSN?
极致效率,云原生数据库TDSQL-C安身立命的根本
What does password security mean? What are the password security standard clauses in the ISO 2.0 policy?
mysql如何修改存储引擎为innodb
曾经,我同时兼职5份工作,只为给女友买个新款耳环......
What are strong and weak symbols in embedded systems?
On the routing tree of gin
Bochs software usage record
好风凭借力 – 使用Babelfish 加速迁移 SQL Server 的代码转换实践
Runmaide medical passed the hearing: Ping An capital was a shareholder with a loss of 630million during the year
数据库连接池:压力测试
NF RESNET: network signal analysis worth reading after removing BN normalization | ICLR 2021
Is pioneer futures reliable? How to open a futures account safely?
[Zhejiang University] information sharing of the first and second postgraduate entrance examinations
推荐几个AI智能平台
壹连科技冲刺深交所:年营收14亿 65%收入来自宁德时代
向量6(继承)
得物技术复杂 C 端项目的重构实践