質問編集履歴

4

倍率に誤りがあったので修正

2020/06/28 15:40

投稿

IShix
IShix

スコア1724

test CHANGED
File without changes
test CHANGED
@@ -18,7 +18,7 @@
18
18
 
19
19
  Math.Pow 0.003 秒
20
20
 
21
- for 0.477
21
+ for 0.047 秒
22
22
 
23
23
 
24
24
 

3

コードの修正

2020/06/28 15:40

投稿

IShix
IShix

スコア1724

test CHANGED
File without changes
test CHANGED
@@ -26,12 +26,6 @@
26
26
 
27
27
  ```C#
28
28
 
29
- using System;
30
-
31
- using UnityEngine.Assertions;
32
-
33
-
34
-
35
29
  public sealed class ItemPrice
36
30
 
37
31
  {
@@ -90,7 +84,7 @@
90
84
 
91
85
 
92
86
 
93
- double Load(int level, long startPrice, double priceRate)
87
+ public static double Load(int level, long startPrice, double priceRate)
94
88
 
95
89
  {
96
90
 
@@ -100,7 +94,7 @@
100
94
 
101
95
 
102
96
 
103
- double Upgrade(double currentPrice, double rate) => currentPrice * rate;
97
+ public static double Upgrade(double currentPrice, double priceRate) => currentPrice * priceRate;
104
98
 
105
99
  }
106
100
 

2

コードの命名修正

2020/06/28 15:31

投稿

IShix
IShix

スコア1724

test CHANGED
File without changes
test CHANGED
@@ -84,7 +84,7 @@
84
84
 
85
85
  Level++;
86
86
 
87
- value = Update(value, PriceRate);
87
+ value = Upgrade(value, PriceRate);
88
88
 
89
89
  }
90
90
 
@@ -100,7 +100,7 @@
100
100
 
101
101
 
102
102
 
103
- double Update(double currentPrice, double rate) => currentPrice * rate;
103
+ double Upgrade(double currentPrice, double rate) => currentPrice * rate;
104
104
 
105
105
  }
106
106
 

1

解決したので追記

2020/06/28 15:27

投稿

IShix
IShix

スコア1724

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,113 @@
1
+ ### [追記] 最終的にいただいた意見をすべて混ぜました
2
+
3
+ ku__ra__geさん、ikadzuchiさん、TN8001さん
4
+
5
+ ありがとうございました。
6
+
7
+
8
+
9
+ ・ 初回ロードでMath.Powを使いキャッシュ
10
+
11
+ ・ 2回目以降はキャッシュに対して計算する
12
+
13
+
14
+
15
+ Math.Powは重いとのことでしたが、計測したところ今回の使い方では早いので使うことにしました。
16
+
17
+ Macbook proにて1万回繰り返した結果
18
+
19
+ Math.Pow 0.003 秒
20
+
21
+ for 0.477 秒
22
+
23
+
24
+
25
+ **コード全文**
26
+
27
+ ```C#
28
+
29
+ using System;
30
+
31
+ using UnityEngine.Assertions;
32
+
33
+
34
+
35
+ public sealed class ItemPrice
36
+
37
+ {
38
+
39
+ public int Level{ get; private set; }
40
+
41
+ public long Value => (long)value;
42
+
43
+
44
+
45
+ public readonly long StartPrice;
46
+
47
+ public readonly double PriceRate;
48
+
49
+
50
+
51
+ double value;
52
+
53
+
54
+
55
+ public ItemPrice(int level, long startPrice, double priceRate)
56
+
57
+ {
58
+
59
+ Assert.IsTrue(level > 0);
60
+
61
+ Assert.IsTrue(startPrice > 0);
62
+
63
+ Assert.IsTrue(priceRate > 0);
64
+
65
+
66
+
67
+ Level = level;
68
+
69
+ StartPrice = startPrice;
70
+
71
+ PriceRate = priceRate;
72
+
73
+
74
+
75
+ value = Load(Level, StartPrice, PriceRate);
76
+
77
+ }
78
+
79
+
80
+
81
+ public void Upgrade()
82
+
83
+ {
84
+
85
+ Level++;
86
+
87
+ value = Update(value, PriceRate);
88
+
89
+ }
90
+
91
+
92
+
93
+ double Load(int level, long startPrice, double priceRate)
94
+
95
+ {
96
+
97
+ return level > 1 ? startPrice * Math.Pow(priceRate, level - 1) : startPrice;
98
+
99
+ }
100
+
101
+
102
+
103
+ double Update(double currentPrice, double rate) => currentPrice * rate;
104
+
105
+ }
106
+
107
+ ```
108
+
109
+
110
+
1
111
  ### 実現したいこと
2
112
 
3
113
  購入するごとに金額が増えていくアイテムがあります。