vendredi 27 octobre 2017

Shaders.metal producing Errors in Swift 4

I am fairly new to swift and have a code as follows which I want to run faster:

for a in 0 ..< max {
  for b in 0 ..< max1 {
    //Do some calculations from which variables c,d and e stem. The calculations are dependent on a and b.
  //Checking if the indices are within the bounds of array.
    if(c>-1 && c<data[0].count && d>-1 && d<data[0][0].count&& e>-1&&e<data.count) {
     array.append(UInt8(data[e][c][d])
      }
      else{
      array.append(UInt(0))
      }
   }
}

From measuring the time needed I found that if and the read from the array cost most time. However the if has the greatest impact. I already tried to use data.indices.contains which brought a little improvement but not yet enough. If I add “array.append(UInt8(0))” and “continue” inside the second for loop as the first statement, everything runs fast enough. Is there a way to speed these things up? Can’t I just catch the error which returns if one of the indices is out of bounds? In Java it is Index out of bounds and I could’ve just catched it. Concerning the data array I tried to make it into a one dimensional array which caused a lot of trouble because I was no more able to check if the indices are inside the bounds of the array.

The code above runs with the extra calculations in about 0.021 s and with the use of a ternary operator instead the if in about 0.019 s. With the use of preinitializing the array to just 0s and deleting the else statement I get an additional improvement to about 0.018 s. Totally I would need it to run in about 0.001s. Is there a way to do it?

I even tried to run it with metal in parallel which was even a little slower ...

Help would be greatly appreciated. 👍

Aucun commentaire:

Enregistrer un commentaire