About the speed in "for" - c#-4.0

There was a task to know which code is faster and why:
//A
Stopwatch sw = new Stopwatch();
Int32[] a = new Int32[10000000];
int len = a.Length;
sw.Start();
//for (Int32 index = 0; index < len; index++)
//{
// a[index] = 6;
//}
//sw.Stop();
//B
for (Int32 index = 0; index < a.Length; index++)
{
a[index] = 6;
}
sw.Stop();
var time = sw.ElapsedMilliseconds;
The avarage time of approach B is little more than A. Does anybody explain why?

My guess would be because you are polling the length of the array each time you loop, whereas the A approach only has to check a cached value of the length. So, ultimately, this comes down to the idea of caching

In case B, each time through the loop, runtime is calling code to get the length of the array.
In A, this is done once and the result saved.
I've always thought that the runtime would optimize that loop and they would be the same. Are your results significantly different? Have you run the test a few time to see that it is consistently different?

Related

Is it possible to parallelize or unroll this loop?

I am trying to see if I can improve the performance of the following loop in C++, which uses two dimensional vectors (_external and _Table) and has a carried loop dependency on the previous iteration. Additionally, it has a calculated index accessor in the innermost loop that will make the access of _Table non sequential on the right hand side.
int N = 8000;
int M = 400
int P = 100;
for(int i = 1; i <= N; i++){
for(int j = 0; j < M; j++){
for(int k =0; k < P; k++){
int index = _external.at(j).at(k);
_Table.at(j).at(i) += _Table.at(index).at(i-1);
}
}
}
What can I do to improve the performance of a loop like this?
Well it looks to me like the order in which these statements:
int index = _external.at(j).at(k);
_Table.at(j).at(i) += _Table.at(index).at(i-1);
are executed is critical to correctness. (That is, if the iteration order for i, j, k changes, then the results will be different ... and incorrect.)
So I think you are only left with micro-optimizations, like hoisting the expressions _Table.at(j).at(i) and _external.at(j) out of the innermost loop.
Consider this:
for(int k =0; k < P; k++){
int index = _external.at(j).at(k);
_Table.at(j).at(i) += _Table.at(index).at(i-1);
}
This loop is repeatedly adding numbers to _Table.at(j).at(i). Since (by inspection) _Table.at(index).at(i-1) must be reading from a different cell of the table (because of i-1 versus i), you could do this:
int temp = 0;
for(int k =0; k < P; k++){
int index = _external.at(j).at(k);
temp += _Table.at(index).at(i-1);
}
_Table.at(j).at(i) += temp;
This will reduce the number of calls to at, and may also improve cache performance a bit.

Assigning a new string crashes?

I'm trying to write my first real program with dynamic arrays, but I've come across a problem I cannot understand. Basically, I am trying to take a dynamic array, copy it into a temporary one, add one more address to the original array, then copy everything back to the original array. Now the original array has one more address than before. This worked perfectly when trying with ints, but strings crash my program. Here's an example of the code I'm struggling with:
void main()
{
int x = 3;
std::string *q;
q = new std::string[x];
q[0] = "1";
q[1] = "2";
q[2] = "3";
x++;
std::string *temp = q;
q = new std::string[x];
q = temp;
q[x-1] = "4";
for (int i = 0; i < 5; i++)
std::cout << q[i] << std::endl;
}
If I were to make q and temp into pointers to int instead of string then the program runs just fine. Any help would be greatly appreciated, I've been stuck on this for an hour or two.
q = temp performs only a shallow copy. You lose the original q and all of the strings it pointed to.
Since you reallocated q to have 4 elements, but then immediately reassigned temp (which was allocated with only 3 elements), accessing (and assigning) the element at x now is outside the bounds of the array.
If you have to do it this way for some reason, it should look like this:
auto temp = q;
q = new std::string[x];
for(int x = 0; x < 3; ++x)
q[x] = temp[x];
delete [] temp;
q[x] = 4;
However, this is obviously more complex and very much more prone to error than the idiomatic way of doing this in C++. Better to use std::vector<std::string> instead.

ArgumentException while reading using readblock streamreader

I am trying to calculate row count from a large file based on presence of a certain character and would like to use StreamReader and ReadBlock - below is my code.
protected virtual long CalculateRowCount(FileStream inStream, int bufferSize)
{
long rowCount=0;
String line;
inStream.Position = 0;
TextReader reader = new StreamReader(inStream);
char[] block = new char[4096];
const int blockSize = 4096;
int indexer = 0;
int charsRead = 0;
long numberOfLines = 0;
int count = 1;
do
{
charsRead = reader.ReadBlock(block, indexer, block.Length * count);
indexer += blockSize ;
numberOfLines = numberOfLines + string.Join("", block).Split(new string[] { "&ENDE" }, StringSplitOptions.None).Length;
count ++;
} while (charsRead == block.Length);//charsRead !=0
reader.Close();
fileRowCount = rowCount;
return rowCount;
}
But I get error
Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
I am not sure what is wrong... Can you help. Thanks ahead!
For one, read the StreamReader.ReadBlock() documentation carefully http://msdn.microsoft.com/en-us/library/system.io.streamreader.readblock.aspx and compare with what you're doing:
The 2nd argument (indexer) should be within the range of the block you're passing in, but you're passing something that will probably exceed it after one iteration. Since it looks like you want to reuse the memory block, pass 0 here.
The 3rd argument (count) indicates how many bytes to read into your memory block; passing something larger than the block size might not work (depends on implementation)
ReadBlock() returns the number of bytes actually read, but you increment indexer as if it will always return the size of the block exactly (most of the time, it won't)

Convert For loop into Parallel.For loop

public void DoSomething(byte[] array, byte[] array2, int start, int counter)
{
int length = array.Length;
int index = 0;
while (count >= needleLen)
{
index = Array.IndexOf(array, array2[0], start, count - length + 1);
int i = 0;
int p = 0;
for (i = 0, p = index; i < length; i++, p++)
{
if (array[p] != array2[i])
{
break;
}
}
Given that your for loop appears to be using a loop body dependent on ordering, it's most likely not a candidate for parallelization.
However, you aren't showing the "work" involved here, so it's difficult to tell what it's doing. Since the loop relies on both i and p, and it appears that they would vary independently, it's unlikely to be rewritten using a simple Parallel.For without reworking or rethinking your algorithm.
In order for a loop body to be a good candidate for parallelization, it typically needs to be order independent, and have no ordering constraints. The fact that you're basing your loop on two independent variables suggests that these requirements are not valid in this algorithm.

How does one jump to a particular offset more than once?

Imagine this function:
void SoundManager::playSource(ALuint sourceID, float offset)
{
alSourceStop(sourceID);
ALint iTotal = 0;
ALint iCurrent = 0;
ALint uiBuffer = 0;
alGetSourcei(sourceID, AL_BUFFER, &uiBuffer);
alGetBufferi(uiBuffer, AL_SIZE, &iTotal);
iCurrent = iTotal * offset;
alSourcei(sourceID, AL_BYTE_OFFSET, iCurrent);
alSourcePlay(sourceID);
}
The idea is calling playSource(x, 0.5f) would jump to (roughly) halfway through the buffer, etc.
It works fine the first time I call it, but if I call it again on the same source (whether that source is playing or not) it begins playing as though I'd called it with offset 0.
Any ideas why?
Solved!
Even though the API claims that setting the offsets works on sources in any state, the problem was I should have been calling alSourceRewind instead of alSourceStop at the start.
It seems setting offsets only works on sources in the AL_INITIAL state.
you can play the audio file first,then call setOffset like this,don't need to call alSourcePlay:
- (BOOL)setOffset:(float)offset
{
ALint iTotal = 0;
ALint iCurrent = 0;
ALint uiBuffer = 0;
alGetSourcei(_sourceID, AL_BUFFER, &uiBuffer);
alGetBufferi(uiBuffer, AL_SIZE, &iTotal);
iCurrent = iTotal * offset;
alSourcei(_sourceID, AL_BYTE_OFFSET, iCurrent);
return ((_error = alGetError()) != AL_NO_ERROR);
}

Resources