Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

can i solve this:
private static string _positions;

//Initialize _positions with '1' depending on items (up to 500 items)
_positions = new string('1', items);

//If a certain condition is fulfilled, then set the index of that specific item to blank
_positions = new StringBuilder(_positions) { [i] = ' ' }.ToString();

//Test _positions for whitespace, then all items handled 
if (string.IsNullOrWhiteSpace(_positions))
{
	DoSomething();
}

with something like BitArray and test for 0 or similar? It should be significantly faster. My problem are the up to 500 items.

Thanks in advance

What I have tried:

This and other known solutions. But my problem is the high amount of items to test for.
Posted
Updated 12-Dec-18 2:34am

Write a class, holding an array of integers (or whatever), say arr. The ctor would set all the array items in default state (say 0) and a counter variable to 0.
Then provide a method to change an item, say setOne(int index).
At each setOne call, if the item is 0 then it is changed to 1 and counter incremented; on the other hand, if it is already 1 then nothing happens.

The 'global empty' test then would reduce to (counter == arr.Length).
 
Share this answer
 
Hi CPallini,

nice solution. Thank you very much! Here is my resulting quick code:

static internal class PositionCheck
{
  static private int _counter;
  static internal int Counter { get => _counter; }
  static private char[] _arr;

  static internal void SetItemCount(int items)
  {
     _arr = new char[items];
     _counter = items;
  }

  static internal void Set(int index)
  {

	 if(_arr[index] == '\0')
	 {
		_arr[index] = '1';
		_counter--;
	 }
  }

  static internal bool Get(int index)
  {
	 return _arr[index] == '\0';
  }
}

Ken
 
Share this answer
 
Comments
CPallini 12-Dec-18 6:12am    
That is, well done. I wonder if using ints instead of chars would speed-up the code (in addition to wasting memory, of course).
By the way, did you perform a benchmark against your original code?
CPallini 12-Dec-18 8:54am    
[On behalf of Ken Guru]
I think your solution is faster. Checking for string.IsNullOrWhiteSpace() alone must be slower, because internally all character positions need to be iterated thru and tested (I think). In your solution only the counter has to be checked for 0, especially since your solution is well scalable and performs even with 100.000 entries. I have not tested whether a change from char to int offers another significant speed advantage.

Ken
Ken Guru 17-Dec-18 5:31am    
I've now testet this with 1000 rounds with 10.000.000!!! elements at a time on a Core i7 8700k @5GHz.

With char[] ~ 48,67 ms
With int[] ~ 52,91 ms

Only from 200.000 elements a value above 0 ms is noticeable in the stopwatch at all.
The original solution is way too slow. 6.500 Elements are as fast as 10.000.000 with this solution.
:)
CPallini 17-Dec-18 5:50am    
Well done. I supposed the opposite. However, apparently 'size matters'.
CPallini 12-Dec-18 8:56am    
Please post replies to comments using the 'Reply' button, instead of adding spurious solutions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900