###efficiency
efficiency
Yes, this can be improved.
One obvious problem is that you're re-computing your average and your standard deviation on every iteration of a loop, even though only the last result (after the last iteration) is ever actually used.
For example:
for (int i = 0; i < arrSize; i++)
{
sum += grades[i];
average = (sum / arrSize);
}
You're computing average
on every iteration, but only need or use the last value you compute. You can compute it once with code like:
for (int i = 0; i < arrSize; i++)
{
sum += grades[i];
}
average = (sum / arrSize);
Your computation of the standard deviation is much the same way.
Use of pow
I'd avoid using pow
to compute a square. It often imposes quite a bit of overhead, so pow(deviation[i], 2)
will often be substantially slower than deviation[i]*deviation[i]
.
Formatting
Looking at the code more generally, you really need to fix your indentation.
std::endl
I would advise against using std::end
. Normally, you just want '\n'
, which also gives you a new line, but will nearly always be (much) faster. In the case above, it won't make much difference, but if you're writing a lot of data to a file (for example) the difference can get very large, very quickly (e.g., a slowdown of 8:1 or 10:1 is fairly typical).
###efficiency
Yes, this can be improved.
One obvious problem is that you're re-computing your average and your standard deviation on every iteration of a loop, even though only the last result (after the last iteration) is ever actually used.
For example:
for (int i = 0; i < arrSize; i++)
{
sum += grades[i];
average = (sum / arrSize);
}
You're computing average
on every iteration, but only need or use the last value you compute. You can compute it once with code like:
for (int i = 0; i < arrSize; i++)
{
sum += grades[i];
}
average = (sum / arrSize);
Your computation of the standard deviation is much the same way.
Use of pow
I'd avoid using pow
to compute a square. It often imposes quite a bit of overhead, so pow(deviation[i], 2)
will often be substantially slower than deviation[i]*deviation[i]
.
Formatting
Looking at the code more generally, you really need to fix your indentation.
std::endl
I would advise against using std::end
. Normally, you just want '\n'
, which also gives you a new line, but will nearly always be (much) faster. In the case above, it won't make much difference, but if you're writing a lot of data to a file (for example) the difference can get very large, very quickly (e.g., a slowdown of 8:1 or 10:1 is fairly typical).
efficiency
Yes, this can be improved.
One obvious problem is that you're re-computing your average and your standard deviation on every iteration of a loop, even though only the last result (after the last iteration) is ever actually used.
For example:
for (int i = 0; i < arrSize; i++)
{
sum += grades[i];
average = (sum / arrSize);
}
You're computing average
on every iteration, but only need or use the last value you compute. You can compute it once with code like:
for (int i = 0; i < arrSize; i++)
{
sum += grades[i];
}
average = (sum / arrSize);
Your computation of the standard deviation is much the same way.
Use of pow
I'd avoid using pow
to compute a square. It often imposes quite a bit of overhead, so pow(deviation[i], 2)
will often be substantially slower than deviation[i]*deviation[i]
.
Formatting
Looking at the code more generally, you really need to fix your indentation.
std::endl
I would advise against using std::end
. Normally, you just want '\n'
, which also gives you a new line, but will nearly always be (much) faster. In the case above, it won't make much difference, but if you're writing a lot of data to a file (for example) the difference can get very large, very quickly (e.g., a slowdown of 8:1 or 10:1 is fairly typical).
###efficiency
Yes, this can be improved.
One obvious problem is that you're re-computing your average and your standard deviation on every iteration of a loop, even though only the last result (after the last iteration) is ever actually used.
For example:
for (int i = 0; i < arrSize; i++)
{
sum += grades[i];
average = (sum / arrSize);
}
You're computing average
on every iteration, but only need or use the last value you compute. You can compute it once with code like:
for (int i = 0; i < arrSize; i++)
{
sum += grades[i];
}
average = (sum / arrSize);
Your computation of the standard deviation is much the same way.
Use of pow
I'd avoid using pow
to compute a square. It often imposes quite a bit of overhead, so pow(deviation[i], 2)
will often be substantially slower than deviation[i]*deviation[i]
.
Formatting
Looking at the code more generally, you really need to fix your indentation.
std::endl
I would advise against using std::end
. Normally, you just want '\n'
, which also gives you a new line, but will nearly always be (much) faster. In the case above, it won't make much difference, but if you're writing a lot of data to a file (for example) the difference can get very large, very quickly (e.g., a slowdown of 8:1 or 10:1 is fairly typical).