Slow for loop

Why does this piece of code run at 300ms? I can’t see how it is doing anything expensive:

	IEnumerator PaintTheTerrain() {
		int aw = Chunk.terrainData.alphamapWidth;
		
		for (int x = 0; x < aw; x++) {
			float normalPosX = (x * 1f) / (aw * 1f);
			for (int y = 0; y < aw; y++) {
				float normalPosY = (y * 1f) / (aw * 1f);
				
				float angle = Chunk.terrainData.GetSteepness (normalPosX, normalPosY);
				float height = Chunk.terrainData.GetInterpolatedHeight (normalPosX, normalPosY);
				Biome Biome = Biomes.GetPoint (x, y);

				if (height < WaterLevel + 2) {
					map [y, x, 2] = 1;
				} else if (angle > 25) {
					map [y, x, 1] = 1;
				} else {
					map [y, x, Biome.SplatIndex] = 1;
				}
			}
			if (x % 10 == 0) {
				progress = (x * 1f) / (aw * 1f);
				yield return null;
			}
		}
		Chunk.terrainData.SetAlphamaps(0,0,map);
	}

Without going deeper into your code, I will guess that the statement “yield return null” is getting called too frequently, which means every iteration is delayed by ‘at least’ one frame. Normally at 30fps, your loop will run after 0.03 (1/30) seconds every time the boolean expression returns true. Try putting a Debug.Log(x); for the value of x and see if the boolean expression returns true more than what you desire.