hi, i used this code:
[BurstCompile]
public struct myJob : IJob
{
[NativeDisableContainerSafetyRestriction]
public NativeArray<int> nativeArray;
public int i;
public void Execute()
{
i=0;
for(int j = 0; j < 500; j++)
{
for(int u = 0; u < 500; u++,i++)
{
if(nativeArray[i] == 1)
{
//do
}
}
}
}
}
where it says //do there’s nothing so its effectively only doing the array check, i put that same for loop on a task and is quite faster, the code is using [BurstCompile].
And here is the task;
await Task.Run(() => {
i=0;
for(int j = 0; j < 500; j++)
{
for(int u = 0; u < 500; u++,i++)
{
if(normalArrayOfInts[i] == 1)
{
//do
}
}
}
});
That task runs quite faster.
To be clear checking the array in the conditional seems to be the problem, u can put the mod back and yul see almost no difference.
To be sure i changed the condition in the job to this as well:
if(2 == 1)
{
//do
}
this in the job takes 0.7 ms, while the array check in the job takes 5ms.
The reason i put the array like that is to simulate a condition check of an array that u can’t avoid, doing the same thing on a task with a normal array is much faster, so it has to be the array check no?
There are tricks to get around these however. You can use Burst compiler hint attribute to say that a conditional is mostly true/false to make it optimize for a specific use case.
You can also sometimes avoid conditionals altogether merely by rephrasing your code. Sometimes it requires running multiple jobs in a sequence so that one or several iterations over the data can be vectorized, while only one job performs the conditionals. And yes, it can be way, way more efficient to run multiple jobs in sequence rather than a single job that does everything even though it goes against nature on first sight seeing that you are iterating over the same data multiple times but that’s just something the CPU enjoys doing way more over testing conditionals and jumping around in memory.
if the array check is the problem then running multiple jobs would be redundant(?), i mean u have to check the array anyway, on each job, the same amount of times, assuming u can’t avoid the check.