Skip to content

Commit fab18b0

Browse files
committed
Add UnitySmoothDamp for comparison
1 parent 285a68a commit fab18b0

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

Documentation~/performance.png

16.2 KB
Loading

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Visualizer:
6464

6565
![](./Documentation~/visualizer.gif)
6666

67-
# Faq
67+
# FAQ
6868

6969
### Unity SmoothDamp
7070

Tests/Editor/PerformanceTest.cs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using NUnit.Framework;
22
using System.Diagnostics;
3+
using UnityEngine;
34
using Debug = UnityEngine.Debug;
45

56
namespace UnitySpring.Tests
@@ -9,21 +10,25 @@ public class PerformanceTest
910
const int Count = 100000;
1011

1112
[Test]
12-
public static void ClosedForm()
13+
public static void _ClosedForm()
1314
=> Benchmark(CreateSprings<ClosedForm.Spring>(), "ClosedForm");
1415

1516
[Test]
16-
public static void SemiImplicitEuler()
17+
public static void _SemiImplicitEuler()
1718
=> Benchmark(CreateSprings<SemiImplicitEuler.Spring>(), "SemiImplicitEuler");
1819

1920
[Test]
20-
public static void ExplicitRK4()
21+
public static void _ExplicitRK4()
2122
=> Benchmark(CreateSprings<ExplicitRK4.Spring>(), "ExplicitRK4");
2223

2324
[Test]
24-
public static void VerletIntegration()
25+
public static void _VerletIntegration()
2526
=> Benchmark(CreateSprings<VerletIntegration.Spring>(), "VerletIntegration");
2627

28+
[Test]
29+
public static void UnitySmoothDamp()
30+
=> Benchmark(CreateSprings<UnitySmoothDamp>(), "UnitySmoothDamp");
31+
2732
static void Benchmark(SpringBase[] springs, string name)
2833
{
2934
var sw = new Stopwatch();
@@ -36,7 +41,7 @@ static void Benchmark(SpringBase[] springs, string name)
3641
count++;
3742
}
3843

39-
Debug.Log($"[{name}] Average time for {Count} spring per frame : {sum / count} ms");
44+
Debug.Log($"[{name}] Average time for {Count} spring per frame : {(sum / count).ToString("0.000")} ms");
4045

4146
double Step()
4247
{
@@ -60,7 +65,31 @@ double Step()
6065

6166
static double GetPreciseTime(Stopwatch sw)
6267
{
63-
return 1000.0 * (double)sw.ElapsedTicks / Stopwatch.Frequency;
68+
return 1000.0 * (double) sw.ElapsedTicks / Stopwatch.Frequency;
69+
}
70+
}
71+
72+
class UnitySmoothDamp : SpringBase
73+
{
74+
float smoothTime = 0.1f;
75+
float maxSpeed = Mathf.Infinity;
76+
77+
public override void Reset()
78+
{
79+
currentValue = startValue;
80+
currentVelocity = initialVelocity;
81+
}
82+
83+
public override void UpdateEndValue(float value, float velocity)
84+
{
85+
endValue = value;
86+
currentVelocity = velocity;
87+
}
88+
89+
public override float Evaluate(float deltaTime)
90+
{
91+
currentValue = Mathf.SmoothDamp(currentValue, endValue, ref currentVelocity, smoothTime, maxSpeed, deltaTime);
92+
return currentValue;
6493
}
6594
}
66-
}
95+
}

0 commit comments

Comments
 (0)