当我想知道哪个更快时,我正在创建和翻译一些算法?
a) (int)float
要么
b) Mathf.FloorToInt(float)
提前致谢。
编辑:如果有比这两者更快的方式,那也会有所帮助。
I'm creating and translating a few algorithms when I'm wondering which is faster?
a) (int)float
or
b) Mathf.FloorToInt(float)
Thanks in advance.
EDIT: If there is a faster way than either of those, that would be helpful too.
最满意答案
像我提到的那样用秒表做测试。 这个答案就在这里,因为我相信你答案的结果是错误的。
下面是一个使用循环的简单性能测试脚本,因为您的算法涉及许多循环:
void Start() { int iterations = 10000000; //TEST 1 Stopwatch stopwatch1 = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { int test1 = (int)0.5f; } stopwatch1.Stop(); //TEST 2 Stopwatch stopwatch2 = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { int test2 = Mathf.FloorToInt(0.5f); } stopwatch2.Stop(); //SHOW RESULT WriteLog(String.Format("(int)float: {0}", stopwatch1.ElapsedMilliseconds)); WriteLog(String.Format("Mathf.FloorToInt: {0}", stopwatch2.ElapsedMilliseconds)); } void WriteLog(string log) { UnityEngine.Debug.Log(log); }输出:
(int)float: 73
Mathf.FloorToInt: 521
(int)float显然比Mathf.FloorToInt快。 对于这样的事情,使用编辑统计中的FPS来判断是非常糟糕的。 你用Stopwatch做测试。 编写着色器代码时应使用FPS。
Do a test with Stopwatch like I mentioned. This answer is here because I believe that the result in your answer is wrong.
Below is a simple performance test script that uses loop since your algorithm involves many loops:
void Start() { int iterations = 10000000; //TEST 1 Stopwatch stopwatch1 = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { int test1 = (int)0.5f; } stopwatch1.Stop(); //TEST 2 Stopwatch stopwatch2 = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { int test2 = Mathf.FloorToInt(0.5f); } stopwatch2.Stop(); //SHOW RESULT WriteLog(String.Format("(int)float: {0}", stopwatch1.ElapsedMilliseconds)); WriteLog(String.Format("Mathf.FloorToInt: {0}", stopwatch2.ElapsedMilliseconds)); } void WriteLog(string log) { UnityEngine.Debug.Log(log); }Output:
(int)float: 73
Mathf.FloorToInt: 521
(int)float is clearly faster than Mathf.FloorToInt. For stuff like this, it is really bad to use the FPS from the Editor Stats to make the judgement. You do a test with the Stopwatch. The FPS should be used when writing shader code.
更多推荐
发布评论