Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Some improvements:

###Combine loops

int maxTopics = 0;
int maxTeams = 0;
for(int i = 0; i < numberOfMembers - 1; i++)
 for (int j = i + 1; j < numberOfMembers; j++)
 {
 int numberOfTeamTopics 
 = NumberOfSetBits(new BitArray(numberOfTopics).Or(bitArray[i]).Or(bitArray[j]));
 if (numberOfTeamTopics > maxTopics)
 {
 maxTopics = numberOfTeamTopics;
 maxTeams = 1;
 }
 else if (numberOfTeamTopics == maxTopics)
 {
 maxTeams++; 
 }
 }

###Use a better algorithm a better algorithm for counting set bits

public static Int32 NumberOfSetBits(BitArray bitArray)
{
 Int32[] ints = new Int32[(bitArray.Count >> 5) + 1];
 bitArray.CopyTo(ints, 0);
 Int32 count = 0;
 // fix for not truncated bits in last integer that may have been set to true with SetAll()
 ints[ints.Length - 1] &= ~(-1 << (bitArray.Count % 32));
 for (Int32 i = 0; i < ints.Length; i++)
 {
 Int32 c = ints[i];
 // magic (http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel)
 unchecked
 {
 c = c - ((c >> 1) & 0x55555555);
 c = (c & 0x33333333) + ((c >> 2) & 0x33333333);
 c = ((c + (c >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
 }
 count += c;
 }
 return count;
}

New execution time: 0.12 seconds.

Some improvements:

###Combine loops

int maxTopics = 0;
int maxTeams = 0;
for(int i = 0; i < numberOfMembers - 1; i++)
 for (int j = i + 1; j < numberOfMembers; j++)
 {
 int numberOfTeamTopics 
 = NumberOfSetBits(new BitArray(numberOfTopics).Or(bitArray[i]).Or(bitArray[j]));
 if (numberOfTeamTopics > maxTopics)
 {
 maxTopics = numberOfTeamTopics;
 maxTeams = 1;
 }
 else if (numberOfTeamTopics == maxTopics)
 {
 maxTeams++; 
 }
 }

###Use a better algorithm for counting set bits

public static Int32 NumberOfSetBits(BitArray bitArray)
{
 Int32[] ints = new Int32[(bitArray.Count >> 5) + 1];
 bitArray.CopyTo(ints, 0);
 Int32 count = 0;
 // fix for not truncated bits in last integer that may have been set to true with SetAll()
 ints[ints.Length - 1] &= ~(-1 << (bitArray.Count % 32));
 for (Int32 i = 0; i < ints.Length; i++)
 {
 Int32 c = ints[i];
 // magic (http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel)
 unchecked
 {
 c = c - ((c >> 1) & 0x55555555);
 c = (c & 0x33333333) + ((c >> 2) & 0x33333333);
 c = ((c + (c >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
 }
 count += c;
 }
 return count;
}

New execution time: 0.12 seconds.

Some improvements:

###Combine loops

int maxTopics = 0;
int maxTeams = 0;
for(int i = 0; i < numberOfMembers - 1; i++)
 for (int j = i + 1; j < numberOfMembers; j++)
 {
 int numberOfTeamTopics 
 = NumberOfSetBits(new BitArray(numberOfTopics).Or(bitArray[i]).Or(bitArray[j]));
 if (numberOfTeamTopics > maxTopics)
 {
 maxTopics = numberOfTeamTopics;
 maxTeams = 1;
 }
 else if (numberOfTeamTopics == maxTopics)
 {
 maxTeams++; 
 }
 }

###Use a better algorithm for counting set bits

public static Int32 NumberOfSetBits(BitArray bitArray)
{
 Int32[] ints = new Int32[(bitArray.Count >> 5) + 1];
 bitArray.CopyTo(ints, 0);
 Int32 count = 0;
 // fix for not truncated bits in last integer that may have been set to true with SetAll()
 ints[ints.Length - 1] &= ~(-1 << (bitArray.Count % 32));
 for (Int32 i = 0; i < ints.Length; i++)
 {
 Int32 c = ints[i];
 // magic (http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel)
 unchecked
 {
 c = c - ((c >> 1) & 0x55555555);
 c = (c & 0x33333333) + ((c >> 2) & 0x33333333);
 c = ((c + (c >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
 }
 count += c;
 }
 return count;
}

New execution time: 0.12 seconds.

added 4 characters in body
Source Link

Some improvements:

###Combine loops

int maxTopics = 0;
int maxTeams = 0;
for(int i=0;i = 0; i < numberOfMembers - 1; i++)
 for (int j=ij = i + 1; j < numberOfMembers; j++)
 {
 int numberOfTeamTopics 
 = NumberOfSetBits(new BitArray(numberOfTopics).Or(bitArray[i]).Or(bitArray[j]));
 if (numberOfTeamTopics > maxTopics)
 {
 maxTopics = numberOfTeamTopics;
 maxTeams = 1;
 }
 else if (numberOfTeamTopics == maxTopics)
 {
 maxTeams++; 
 }
 }

###Use a better algorithm for counting set bits

public static Int32 NumberOfSetBits(BitArray bitArray)
{
 Int32[] ints = new Int32[(bitArray.Count >> 5) + 1];
 bitArray.CopyTo(ints, 0);
 Int32 count = 0;
 // fix for not truncated bits in last integer that may have been set to true with SetAll()
 ints[ints.Length - 1] &= ~(-1 << (bitArray.Count % 32));
 for (Int32 i = 0; i < ints.Length; i++)
 {
 Int32 c = ints[i];
 // magic (http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel)
 unchecked
 {
 c = c - ((c >> 1) & 0x55555555);
 c = (c & 0x33333333) + ((c >> 2) & 0x33333333);
 c = ((c + (c >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
 }
 count += c;
 }
 return count;
}

New execution time: 0.12 seconds.

Some improvements:

###Combine loops

int maxTopics = 0;
int maxTeams = 0;
for(int i=0; i < numberOfMembers - 1; i++)
 for (int j=i + 1; j < numberOfMembers; j++)
 {
 int numberOfTeamTopics 
 = NumberOfSetBits(new BitArray(numberOfTopics).Or(bitArray[i]).Or(bitArray[j]));
 if (numberOfTeamTopics > maxTopics)
 {
 maxTopics = numberOfTeamTopics;
 maxTeams = 1;
 }
 else if (numberOfTeamTopics == maxTopics)
 {
 maxTeams++; 
 }
 }

###Use a better algorithm for counting set bits

public static Int32 NumberOfSetBits(BitArray bitArray)
{
 Int32[] ints = new Int32[(bitArray.Count >> 5) + 1];
 bitArray.CopyTo(ints, 0);
 Int32 count = 0;
 // fix for not truncated bits in last integer that may have been set to true with SetAll()
 ints[ints.Length - 1] &= ~(-1 << (bitArray.Count % 32));
 for (Int32 i = 0; i < ints.Length; i++)
 {
 Int32 c = ints[i];
 // magic (http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel)
 unchecked
 {
 c = c - ((c >> 1) & 0x55555555);
 c = (c & 0x33333333) + ((c >> 2) & 0x33333333);
 c = ((c + (c >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
 }
 count += c;
 }
 return count;
}

New execution time: 0.12 seconds.

Some improvements:

###Combine loops

int maxTopics = 0;
int maxTeams = 0;
for(int i = 0; i < numberOfMembers - 1; i++)
 for (int j = i + 1; j < numberOfMembers; j++)
 {
 int numberOfTeamTopics 
 = NumberOfSetBits(new BitArray(numberOfTopics).Or(bitArray[i]).Or(bitArray[j]));
 if (numberOfTeamTopics > maxTopics)
 {
 maxTopics = numberOfTeamTopics;
 maxTeams = 1;
 }
 else if (numberOfTeamTopics == maxTopics)
 {
 maxTeams++; 
 }
 }

###Use a better algorithm for counting set bits

public static Int32 NumberOfSetBits(BitArray bitArray)
{
 Int32[] ints = new Int32[(bitArray.Count >> 5) + 1];
 bitArray.CopyTo(ints, 0);
 Int32 count = 0;
 // fix for not truncated bits in last integer that may have been set to true with SetAll()
 ints[ints.Length - 1] &= ~(-1 << (bitArray.Count % 32));
 for (Int32 i = 0; i < ints.Length; i++)
 {
 Int32 c = ints[i];
 // magic (http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel)
 unchecked
 {
 c = c - ((c >> 1) & 0x55555555);
 c = (c & 0x33333333) + ((c >> 2) & 0x33333333);
 c = ((c + (c >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
 }
 count += c;
 }
 return count;
}

New execution time: 0.12 seconds.

Source Link

Some improvements:

###Combine loops

int maxTopics = 0;
int maxTeams = 0;
for(int i=0; i < numberOfMembers - 1; i++)
 for (int j=i + 1; j < numberOfMembers; j++)
 {
 int numberOfTeamTopics 
 = NumberOfSetBits(new BitArray(numberOfTopics).Or(bitArray[i]).Or(bitArray[j]));
 if (numberOfTeamTopics > maxTopics)
 {
 maxTopics = numberOfTeamTopics;
 maxTeams = 1;
 }
 else if (numberOfTeamTopics == maxTopics)
 {
 maxTeams++; 
 }
 }

###Use a better algorithm for counting set bits

public static Int32 NumberOfSetBits(BitArray bitArray)
{
 Int32[] ints = new Int32[(bitArray.Count >> 5) + 1];
 bitArray.CopyTo(ints, 0);
 Int32 count = 0;
 // fix for not truncated bits in last integer that may have been set to true with SetAll()
 ints[ints.Length - 1] &= ~(-1 << (bitArray.Count % 32));
 for (Int32 i = 0; i < ints.Length; i++)
 {
 Int32 c = ints[i];
 // magic (http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel)
 unchecked
 {
 c = c - ((c >> 1) & 0x55555555);
 c = (c & 0x33333333) + ((c >> 2) & 0x33333333);
 c = ((c + (c >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
 }
 count += c;
 }
 return count;
}

New execution time: 0.12 seconds.

lang-cs

AltStyle によって変換されたページ (->オリジナル) /