|
Nelek wrote: don't forget your coat
and your coconut oil.
|
|
|
|
|
If you can keep your head while those about you are losing theirs, perhaps you don't understand the situation.
|
|
|
|
|
Do you ever hate yourself a little for the code you write?
I'm writing some extremely destructive code at the moment
IList<IList<string>> _Explode(IList<IList<string>> left, IList<IList<string>> right)
{
if (right.IsNullOrEmpty())
return left;
else if (left.IsNullOrEmpty())
return right;
var result = new List<IList<string>>();
foreach (var leftList in left)
{
foreach (var rightList in right)
{
var finalList = new List<string>();
finalList.AddRange(leftList);
finalList.AddRange(rightList); ;
if (!result.Contains(
finalList,
OrderedCollectionEqualityComparer<string>.Default))
result.Add(finalList);
}
}
return result;
}
Its job apparently is to work my CPU like a rented mule while it eats up all my ram.
I am microsoft.
EDIT:
After some suggestions in the comments, it didn't make a big difference overall but I removed every foreach and added preallocation to the lists. Comments removed for brevity
IList<IList<string>> _Explode(IList<IList<string>> left, IList<IList<string>> right)
{
if (null== right||0==right.Count)
return left;
else if (null==left||0==left.Count)
return right;
var result = new List<IList<string>>();
for(int ic=left.Count,i=0;i<ic;++i)
{
var leftList = left[i];
for(int jc=right.Count,j=0;j<jc;++j)
{
var rightList = right[j];
var finalList = new List<string>(ic+jc);
for(int kc=leftList.Count,k=0;k<kc;++k)
finalList.Add(leftList[k]);
for (int kc = rightList.Count, k = 0; k < kc; ++k)
finalList.Add(rightList[k]);
var oec = OrderedCollectionEqualityComparer<string>.Default;
var found = false;
for (int kc = result.Count, k = 0; k < kc; ++k)
{
if (oec.Equals(result[k], finalList))
{
found = true;
break;
}
}
if(!found)
result.Add(finalList);
}
}
return result;
}
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
modified 19-Jul-19 12:24pm.
|
|
|
|
|
Why are left and right of type IList<string> ? Should the caller expect their contents to be mutable? It seems they should of type IEnumerable<string> .
#BetterLivingThroughSafeCode
/ravi
|
|
|
|
|
You're absolutely right, but unfortunately .NET's generics are not covariant.
So I could not pass a
List<IList<string>> to something that took
IEnumerable<IEnumerable<string>> or even
IList<IEnumerable<string>> - it doesn't fly.
If .NET's covariance worked more like C++'s I'd have done like you suggested.
But as it is, utility beats theory at the end of the day.
I think of it as a workaround for a limitation of .NET
The other option is to copy them but this method is resource hungry enough
Fortunately, grotty as it is, it's a private method.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
modified 19-Jul-19 11:29am.
|
|
|
|
|
codewitch honey crisis wrote: So I could not pass a
List<IList<string>> to something that took
IEnumerable<IEnumerable<string>> Yes you can.
Generic covariance | C# Online Compiler | .NET Fiddle[^]
codewitch honey crisis wrote: or even
IList<IEnumerable<string>> That one won't work, because IList<T> is invariant on T .
But you could pass it to something that expected an
IReadOnlyList<IEnumerable<string>>
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
i just tried that. my mistake. is this new? the rules for generic covariance i thought i was going by were published back in the 2.0 days. I assumed it hadn't really changed. Or maybe it always worked and I misunderstood it or misremembered it later. Oh well. Thanks.
I probably won't use IReadOnlyList because it's too much churn in my code. If this were a public method I'd consider it.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
Generic co- and contra-variance[^] were first added in .NET 4.0, so your example wouldn't have worked in 2.0.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
That explains why i didn't know. I left the field professionally prior to the wide adoption of .NET 4.
What a cool feature to add. I'll have to remember that. Thanks for that, and the link.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
Ugh, try using for rather than foreach.
|
|
|
|
|
i don't think a couple of spurious enumerator creations are the real perf bottleneck here.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
I've said it before and I'll copy/paste it again:
"
always remember and never forget that the readability of foreach comes at the cost of being slower than for -- that really is a 20x difference
"
YMMV
|
|
|
|
|
in my experience the 20x difference comes when you're doing lots and lots of nested foreach operations, like doing a ton of functional style queries, due to the heap pressure of all those object creation and destructions.
in truth, that function was whipped together in a few minutes so I just didn't bother with for loops.
I only think about performance in terms of linear vs log times - basically mathematical complexity of functions.
Beyond that, I profile. So for code like this it gets changed if and when it needs to be.
However, given the nested nature of this call it is probably best to change it to for loops early on.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
One quick improvement: pre-allocate the inner List<string> to the correct size:
var finalList = new List<string>(leftList.Count + rightList.Count);
That way, it doesn't have to dynamically reallocate the backing array every time it runs out of space.
(It probably won't make a huge difference when you're calling AddRange with something that implements ICollection<T> , but every little helps.)
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
i tried that, and it didn't change things. I think it's because the list sizes are usually only one or two elements and the min starting capacity is higher than that.
At least with ArrayList i remember it being something like 16 but I don't remember looking at List's sourcecode or disassembly so I couldn't say.
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
Add a Thread.Sleep in the inner loop to cool things down a bit.
|
|
|
|
|
I think my better option is to reduce the number of times I'm calling it. =)
and probably split it across multiple cores
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
Your biggest sin is not using brackets around your (now single line) if and for statements...
It's a bug waiting to happen (learned this the hard way)
BURN THE WICKED WITCH!!!
|
|
|
|
|
ugh i hate extra brackets
When I was growin' up, I was the smartest kid I knew. Maybe that was just because I didn't know that many kids. All I know is now I feel the opposite.
|
|
|
|
|
Was the French Revolution a European Ferris wheel?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
No, a French Revolution is what riders in Le Tour make millions of.
Whenever you find yourself on the side of the majority, it is time to pause and reflect. - Mark Twain
|
|
|
|
|
Eye don't think so since it ended up in London.
Socialism is the Axe Body Spray of political ideologies: It never does what it claims to do, but people too young to know better keep buying it anyway. (Glenn Reynolds)
|
|
|
|
|
I'm betting it was nowhere near as fun. Depending on who you might have been.
|
|
|
|
|
OTOH:
defending your castle with boiling oil/tar,
pouring it onto the revolting peasants below,
who knew 2 horrible items could be combined into something so nice:
french fries.
Message Signature
(Click to edit ->)
|
|
|
|
|
Then this upcoming (December) movie may be just the ticket to your own funeral you've been saving up for: [^].
Think how satisfied you will be with the precious gift of life, as you depart this vale of sorrows in which so many kittens die every day, knowing your cats are well provided for after your exit, and that Sekhmet, the protective aspect of Bastet, will guard you through your passage in the underworld as you move toward judgement in the Hall of Maat.
This sweet satisfaction: consider how even more honeyed it shall be knowing that you will never see the abomination known as Taylor Swift degrading the sacred nature of Catness with her dreadful mixture of botox, off-key singing, spastic dancing, and paparazzi fueled sneering self-intoxication !
Surely your soul will pass the test of being weighed against the feather of Maat, and you will be allowed rebirth.
Meow !
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|