当前位置:网站首页>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会把递归和尾递归分别标识出来,点个赞
边栏推荐
- 快速排序quick_sort
- I rely on the sideline to buy a house in full one year: the industry you despise will make a lot of money in the next ten years!
- Common operations in Visual Studio development
- 新版负载均衡WebClient CRUD
- Are there many unemployed people in 2022? Is it particularly difficult to find a job this year?
- 乱解码nlp
- keil MDK 中使用虚拟串口调试串口
- 极致效率,云原生数据库TDSQL-C安身立命的根本
- All famous network platforms in the world
- 再次认识 WebAssembly
猜你喜欢

At 19:00 this Thursday evening, the 7th live broadcast of battle code Pioneer - how third-party application developers contribute to open source

数据资产管理:数据发现,发现什么,怎么发现?

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

“软件定义世界,开源共筑未来” 2022开放原子全球开源峰会7月底即将开启

DevSecOps: CI/CD 流水线安全的最佳实践

那些没考上大学的人,后来过的怎样

On the routing tree of gin

Token processing during API encapsulation

The 12 SQL optimization schemes summarized by professional "brick moving" old drivers are very practical!

加密市场进入寒冬,是“天灾”还是“人祸”?
随机推荐
Sword finger offer46 -- translate numbers into strings
『忘了再学』Shell流程控制 — 38、while循环和until循环介绍
Please, don't be brainwashed. This is the living reality of 90% of Chinese people
Phpstudy 2016 build Pikachu range
天安科技IPO被终止:曾拟募资3.5亿 复星与九鼎是股东
What does password security mean? What are the password security standard clauses in the ISO 2.0 policy?
推荐几个AI智能平台
山东泰安“6·21”燃气爆炸事故后续:全面排查整治餐饮场所燃气安全隐患
ROS2前置基础教程 | 使用CMakeLists.txt编译ROS2节点
Ros2 pre basic tutorial | Xiaoyu teaches you how to use cmake dependency lookup process
"Forget to learn again" shell process control - 38. Introduction to while loop and until loop
FPGA collects DHT11 temperature and humidity
Yilian technology rushes to Shenzhen Stock Exchange: annual revenue of RMB 1.4 billion, 65% of which comes from Ningde times
阿里云发布CIPU,对于企业客户意味着什么?
Bochs software usage record
On the routing tree of gin
U++ operator learning notes
Ultimate efficiency is the foundation for the cloud native database tdsql-c to settle down
MongoDB在腾讯零售优码中的应用
KEIL仿真和vspd