序
并行for循環(huán)
計算著色器里可以弄,但是那個得先了解一堆api,比如什么setBuffer
unity 的 job system好像也可以弄,但是那個也得先了解一堆api
這些都是大而全的,有沒有那種,沒那么神通廣大但是比較容易上手的?
就像C++的openmp,加幾行就行了。
unity與c#與多線程
(84條消息) Unity多線程知識點記錄_unity 多線程_被代碼折磨的狗子的博客-CSDN博客
總的來說,可以用。?
C#如何并行執(zhí)行for循環(huán)?
Parallel For Loop in C# with Examples - Dot Net Tutorials
一個圖就說明白了:
搞點代碼,更具體一些。
// 串行的for循環(huán)
using System;
namespace ParallelProgrammingDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("C# For Loop");
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
// 并行的for循環(huán)【只要稍微改一改,串行for就變成并行for了】
using System;
using System.Threading.Tasks; // 01.加一個頭文件
namespace ParallelProgrammingDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("C# Parallel For Loop");
//It will start from 1 until 10
//Here 1 is the start index which is Inclusive
//Here 11 us the end index which is Exclusive
//Here number is similar to i of our standard for loop
//The value will be store in the variable number
Parallel.For(1, 11, number => {
Console.WriteLine(number);
}); // 02.修改一下for循環(huán)的寫法
Console.ReadLine();
}
}
}
插樁與計時
可以用這種方法,來統(tǒng)計時間,判斷并行執(zhí)行的for循環(huán)對效率有沒有提升。?
(84條消息) c#統(tǒng)計代碼執(zhí)行時間_c# 代碼執(zhí)行時間_jenny_paofu的博客-CSDN博客文章來源:http://www.zghlxwxcb.cn/news/detail-468048.html
System.DateTime start = System.DateTime.Now;
//耗時的for循環(huán)
System.DateTime end = System.DateTime.Now;
print("用時:" + (end - start));
后記
這是比較簡單的一種寫法,當(dāng)然局限也很多:文章來源地址http://www.zghlxwxcb.cn/news/detail-468048.html
- CPU的核本來就遠(yuǎn)遠(yuǎn)少于GPU
- 這里沒考慮加鎖等問題
到了這里,關(guān)于[unity]如何并行for循環(huán)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!