Click here to Skip to main content
15,887,596 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
JokeRe: What use are foreign keys anyway? Pin
phil.o19-Feb-18 12:15
professionalphil.o19-Feb-18 12:15 
GeneralRe: What use are foreign keys anyway? Pin
Inferno9019-Feb-18 16:45
professionalInferno9019-Feb-18 16:45 
GeneralRe: What use are foreign keys anyway? Pin
Richard Deeming20-Feb-18 2:42
mveRichard Deeming20-Feb-18 2:42 
GeneralRe: What use are foreign keys anyway? Pin
kmoorevs23-Feb-18 5:43
kmoorevs23-Feb-18 5:43 
GeneralRe: What use are foreign keys anyway? Pin
Rob Grainger27-Feb-18 23:20
Rob Grainger27-Feb-18 23:20 
GeneralRe: What use are foreign keys anyway? Pin
TheGreatAndPowerfulOz5-Mar-18 8:47
TheGreatAndPowerfulOz5-Mar-18 8:47 
GeneralRe: What use are foreign keys anyway? Pin
HarvestMoon000010-Mar-18 7:02
HarvestMoon000010-Mar-18 7:02 
GeneralConcurrentQueue & FirstOrDefault() and search speed. Pin
raddevus5-Feb-18 10:28
mvaraddevus5-Feb-18 10:28 
You probably all knew this, but I needed a definitive answer.

I'm using a ConcurrentQueue which is a FIFO collection.
I needed to know that when I went to determine if the queue already had the item, if it would return faster when the item was found at the top (next item out) versus found at the bottom (last out position).

I assumed, but wasn't sure.
This code gives a good definitive answer.
C#
ConcurrentQueue<int> mainList = new ConcurrentQueue<int>();
Console.WriteLine("Adding items...");
for (int x = 1; x < 10000000;x++){
    mainList.Enqueue(x);
}
Console.WriteLine("DONE adding items...");
Stopwatch sw = new Stopwatch();

Console.WriteLine("Searching for FIRST item...");
sw.Start();
mainList.FirstOrDefault(item => item == 1);
sw.Stop();
Console.WriteLine ("{0}ms",sw.ElapsedMilliseconds);
sw = new StopWatch();
Console.WriteLine("Searching for LAST item...");
sw.Start();
mainList.FirstOrDefault(item => item == 9999999);
sw.Stop();
Console.WriteLine ("{0}ms",sw.ElapsedMilliseconds);

Output
Adding items...
DONE adding items...
Searching for FIRST item...
0ms
Searching for LAST item...
99ms

The consistent result is around 100ms on my machine.
EDIT
This addition seems to bear expected results too:

C#
for (int z = 9999999; z >= 1;z--){
    if (z % 1000000 == 0) {
	sw = new Stopwatch();	
	sw.Start();
	mainList.FirstOrDefault(item => item == z);
	sw.Stop();
	Console.WriteLine ("{0} : {1}ms",z, sw.ElapsedMilliseconds);
	}
}

Output for this section is:
9000000 : 99ms
8000000 : 89ms
7000000 : 78ms
6000000 : 71ms
5000000 : 55ms
4000000 : 44ms
3000000 : 33ms
2000000 : 21ms
1000000 : 10ms

Interesting that on my machine it is about 10ms per 1,000,000 items.

EDIT 2

But, wait...there's more!
What about for objects?
I'm creating a queue of anonymous objects via dynamic -- basically cuz I'm lazy.

Then I test to see how long it takes to find the matching value. Answer: Longer
Right, we all assumed longer. The amount of time could just be unboxing of object to get value or whatever.

This answers the question for me though, because I'm going to be putting a bunch of items in a ConcurrentQueue and I want to add more items but don't want to add an item if the collection already contains the item.

C#
void Main()
{
	
	ConcurrentQueue<dynamic> mainList = new ConcurrentQueue<dynamic>();
	Console.WriteLine("Adding items...");
	for (int x = 1; x < 10000000;x++){
		mainList.Enqueue(new {x=x,name="x_" + x});
	}
	Console.WriteLine("DONE adding items...");
	Stopwatch sw = new Stopwatch();
	

	Console.WriteLine("Searching for FIRST item...");
	sw.Start();
	mainList.FirstOrDefault(item => item.x == 1);
	sw.Stop();
	Console.WriteLine ("{0}ms",sw.ElapsedMilliseconds);
	
	
	sw = new Stopwatch();
	Console.WriteLine("Searching for LAST item...");
	sw.Start();
	mainList.FirstOrDefault(item => item.x == 9999999);
	sw.Stop();
	Console.WriteLine ("{0}ms",sw.ElapsedMilliseconds);	

	
	for (int z = 9999999; z >= 1;z--){
		
		if (z % 1000000 == 0) {
			sw = new Stopwatch();	
			sw.Start();
			mainList.FirstOrDefault(item => item.x == z);
			sw.Stop();
			Console.WriteLine ("{0} : {1}ms",z, sw.ElapsedMilliseconds);
			}
		}
}


Output Looks Like
Adding items...
DONE adding items...
Searching for FIRST item...
4ms
Searching for LAST item...
753ms
9000000 : 643ms
8000000 : 575ms
7000000 : 496ms
6000000 : 430ms
5000000 : 362ms
4000000 : 285ms
3000000 : 217ms
2000000 : 141ms
1000000 : 72ms

modified 5-Feb-18 17:25pm.

GeneralRe: ConcurrentQueue & FirstOrDefault() and search speed. Pin
Eddy Vluggen6-Feb-18 1:02
professionalEddy Vluggen6-Feb-18 1:02 
GeneralRe: ConcurrentQueue & FirstOrDefault() and search speed. Pin
Richard Deeming6-Feb-18 2:46
mveRichard Deeming6-Feb-18 2:46 
GeneralRe: ConcurrentQueue & FirstOrDefault() and search speed. Pin
raddevus6-Feb-18 5:41
mvaraddevus6-Feb-18 5:41 
GeneralRe: ConcurrentQueue & FirstOrDefault() and search speed. Pin
Kornfeld Eliyahu Peter6-Feb-18 3:37
professionalKornfeld Eliyahu Peter6-Feb-18 3:37 
GeneralRe: ConcurrentQueue & FirstOrDefault() and search speed. Pin
raddevus6-Feb-18 5:37
mvaraddevus6-Feb-18 5:37 
GeneralRe: ConcurrentQueue & FirstOrDefault() and search speed. Pin
Richard Deeming6-Feb-18 10:11
mveRichard Deeming6-Feb-18 10:11 
GeneralRe: ConcurrentQueue & FirstOrDefault() and search speed. Pin
raddevus6-Feb-18 10:19
mvaraddevus6-Feb-18 10:19 
GeneralRe: ConcurrentQueue & FirstOrDefault() and search speed. Pin
Jörgen Andersson9-Feb-18 2:02
professionalJörgen Andersson9-Feb-18 2:02 
GeneralRe: ConcurrentQueue & FirstOrDefault() and search speed. Pin
raddevus9-Feb-18 4:16
mvaraddevus9-Feb-18 4:16 
GeneralRe: ConcurrentQueue & FirstOrDefault() and search speed. Pin
Richard Deeming9-Feb-18 4:47
mveRichard Deeming9-Feb-18 4:47 
GeneralRe: ConcurrentQueue & FirstOrDefault() and search speed. Pin
raddevus9-Feb-18 8:31
mvaraddevus9-Feb-18 8:31 
GeneralRe: ConcurrentQueue & FirstOrDefault() and search speed. Pin
Richard Deeming12-Feb-18 8:19
mveRichard Deeming12-Feb-18 8:19 
GeneralRe: ConcurrentQueue & FirstOrDefault() and search speed. Pin
Jörgen Andersson9-Feb-18 6:09
professionalJörgen Andersson9-Feb-18 6:09 
GeneralRe: ConcurrentQueue & FirstOrDefault() and search speed. Pin
patbob19-Feb-18 5:42
patbob19-Feb-18 5:42 
Generalsql maths Pin
kmoorevs27-Jan-18 7:17
kmoorevs27-Jan-18 7:17 
GeneralRe: sql maths Pin
RedDk28-Jan-18 11:12
RedDk28-Jan-18 11:12 
GeneralRe: sql maths Pin
kmoorevs29-Jan-18 2:53
kmoorevs29-Jan-18 2:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.