List.RemoveAll() efficiency / compiler optimisation The 2019 Stack Overflow Developer Survey Results Are InHow do I check if a list is empty?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow to make a flat list out of list of lists?How to extend an existing JavaScript array with another array, without creating a new arrayHow do I get the number of elements in a list in Python?How do I concatenate two lists in Python?How to clone or copy a list?Swift Beta performance: sorting arraysReplacing a 32-bit loop counter with 64-bit introduces crazy performance deviations
Why hard-Brexiteers don't insist on a hard border to prevent illegal immigration after Brexit?
FPGA - DIY Programming
Worn-tile Scrabble
Delete all lines which don't have n characters before delimiter
Is this app Icon Browser Safe/Legit?
Multiply Two Integer Polynomials
Why didn't the Event Horizon Telescope team mention Sagittarius A*?
Can someone be penalized for an "unlawful" act if no penalty is specified?
Am I thawing this London Broil safely?
Are there incongruent pythagorean triangles with the same perimeter and same area?
For what reasons would an animal species NOT cross a *horizontal* land bridge?
What does Linus Torvalds mean when he says that Git "never ever" tracks a file?
If a Druid sees an animal’s corpse, can they wild shape into that animal?
How to deal with fear of taking dependencies
How to answer pointed "are you quitting" questioning when I don't want them to suspect
Output the Arecibo Message
Do these rules for Critical Successes and Critical Failures seem Fair?
What is the closest word meaning "respect for time / mindful"
Can a flute soloist sit?
How to save as into a customized destination on macOS?
Did Section 31 appear in Star Trek: The Next Generation?
What do hard-Brexiteers want with respect to the Irish border?
Protecting Dualbooting Windows from dangerous code (like rm -rf)
Is an up-to-date browser secure on an out-of-date OS?
List.RemoveAll() efficiency / compiler optimisation
The 2019 Stack Overflow Developer Survey Results Are InHow do I check if a list is empty?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow to make a flat list out of list of lists?How to extend an existing JavaScript array with another array, without creating a new arrayHow do I get the number of elements in a list in Python?How do I concatenate two lists in Python?How to clone or copy a list?Swift Beta performance: sorting arraysReplacing a 32-bit loop counter with 64-bit introduces crazy performance deviations
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Regarding efficiency, does anyone know if the compiler is clever enough to not create the array containing 1, 3, 5
for each iteration of the loop in the following code?
var foo = new List<int> 1, 2, 3, 4, 5 ;
foo.RemoveAll(i => new[] 1, 3, 5 .Contains(i));
I prefer it for readability, but not at the sake of performance.
c# arrays list linq compiler-optimization
add a comment |
Regarding efficiency, does anyone know if the compiler is clever enough to not create the array containing 1, 3, 5
for each iteration of the loop in the following code?
var foo = new List<int> 1, 2, 3, 4, 5 ;
foo.RemoveAll(i => new[] 1, 3, 5 .Contains(i));
I prefer it for readability, but not at the sake of performance.
c# arrays list linq compiler-optimization
25
try for your self sharplab.io
– Michael Randall
Apr 5 at 8:33
1
@MichaelRandall Great site. You should write an answer for this with an short example. This is interesseting for everybody, i think.
– kara
Apr 5 at 8:47
1
I'm really surprised but it looks like it does actually create a new instance of the 3 item array for each iteration inside the methodinternal bool <M>b__0_0(int i)
.
– maxp
Apr 5 at 8:50
add a comment |
Regarding efficiency, does anyone know if the compiler is clever enough to not create the array containing 1, 3, 5
for each iteration of the loop in the following code?
var foo = new List<int> 1, 2, 3, 4, 5 ;
foo.RemoveAll(i => new[] 1, 3, 5 .Contains(i));
I prefer it for readability, but not at the sake of performance.
c# arrays list linq compiler-optimization
Regarding efficiency, does anyone know if the compiler is clever enough to not create the array containing 1, 3, 5
for each iteration of the loop in the following code?
var foo = new List<int> 1, 2, 3, 4, 5 ;
foo.RemoveAll(i => new[] 1, 3, 5 .Contains(i));
I prefer it for readability, but not at the sake of performance.
c# arrays list linq compiler-optimization
c# arrays list linq compiler-optimization
edited Apr 5 at 21:31
Michael Randall
37.9k84473
37.9k84473
asked Apr 5 at 8:31
maxpmaxp
9,92735105175
9,92735105175
25
try for your self sharplab.io
– Michael Randall
Apr 5 at 8:33
1
@MichaelRandall Great site. You should write an answer for this with an short example. This is interesseting for everybody, i think.
– kara
Apr 5 at 8:47
1
I'm really surprised but it looks like it does actually create a new instance of the 3 item array for each iteration inside the methodinternal bool <M>b__0_0(int i)
.
– maxp
Apr 5 at 8:50
add a comment |
25
try for your self sharplab.io
– Michael Randall
Apr 5 at 8:33
1
@MichaelRandall Great site. You should write an answer for this with an short example. This is interesseting for everybody, i think.
– kara
Apr 5 at 8:47
1
I'm really surprised but it looks like it does actually create a new instance of the 3 item array for each iteration inside the methodinternal bool <M>b__0_0(int i)
.
– maxp
Apr 5 at 8:50
25
25
try for your self sharplab.io
– Michael Randall
Apr 5 at 8:33
try for your self sharplab.io
– Michael Randall
Apr 5 at 8:33
1
1
@MichaelRandall Great site. You should write an answer for this with an short example. This is interesseting for everybody, i think.
– kara
Apr 5 at 8:47
@MichaelRandall Great site. You should write an answer for this with an short example. This is interesseting for everybody, i think.
– kara
Apr 5 at 8:47
1
1
I'm really surprised but it looks like it does actually create a new instance of the 3 item array for each iteration inside the method
internal bool <M>b__0_0(int i)
.– maxp
Apr 5 at 8:50
I'm really surprised but it looks like it does actually create a new instance of the 3 item array for each iteration inside the method
internal bool <M>b__0_0(int i)
.– maxp
Apr 5 at 8:50
add a comment |
3 Answers
3
active
oldest
votes
The answer is no it doesn't optimize out the allocation of the array
Basically, every time the predicate is called, it checks against the compiler generated class and initializes a new array to call the Contains
(as you can see here)
private sealed class <>c
public static readonly <>c <>9 = new <>c();
public static Predicate<int> <>9__0_0;
internal bool <M>b__0_0(int i)
// bam!
int[] obj = new int[3];
RuntimeHelpers.InitializeArray(obj, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
return Enumerable.Contains(obj, i);
add a comment |
As @Michael Randall already wrote, it looks like it is not possible.
I agree, that your questioned code is nicely readable, having the list in the RemoveAll method. But to have the instance only once, I have three ideas of doing it:
int[] a = null;
foo.RemoveAll(i => (a ?? (a = new[] 1, 3, 5 )).Contains(i));
This is actually yours, with little un-beatuness of needing an external variable.
foo = foo.Except(new[] 1, 3, 5 ).ToList();
That's actually pretty nice solution using Linq.
new List<int>1, 3, 5.ForEach(x => foo.Remove(x));
new[] 1, 3, 5.Iterate(x => foo.Remove(x));
This is something I'd do. In neary all of my code I have my Extension method "Iterate" to avoid the need of foreach. And also, i dont want to "toList" everything all the time to make a .ForEach(..)
static class Extensions
public static void Iterate<TSource>(this IEnumerable<TSource> source, Action<TSource> action)
foreach (var item in source)
action.Invoke(item);
6
Beware,RemoveAll
performs vastly better that multiple calls toRemove
.
– Theodor Zoulias
Apr 5 at 9:42
add a comment |
Since the compiler is not that smart, we must outsmart him.
var foo = new List<int> 1, 2, 3, 4, 5 ;
var bar = new HashSet<int>() 1, 3, 5 ;
foo.RemoveAll(i => bar.Contains(i));
1
A hash set of 3 entries will surely not perform better, it would arguably (I have not measured) perform worse.
– Sefe
Apr 5 at 12:22
3
@Sefe While there is probably a difference between the hash set and the array, I think the point of this answer was to createbar
outside of the anonymous function so that's it's created only once and to avoid the creation overhead.
– Joshua Taylor
Apr 5 at 12:42
2
@Sefe what you say is true, but I think that the OP provided a trivial example just to illustrate a point. In a real world scenario the lists will be probably longer than that.
– Theodor Zoulias
Apr 5 at 17:17
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55531327%2flistt-removeall-efficiency-compiler-optimisation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The answer is no it doesn't optimize out the allocation of the array
Basically, every time the predicate is called, it checks against the compiler generated class and initializes a new array to call the Contains
(as you can see here)
private sealed class <>c
public static readonly <>c <>9 = new <>c();
public static Predicate<int> <>9__0_0;
internal bool <M>b__0_0(int i)
// bam!
int[] obj = new int[3];
RuntimeHelpers.InitializeArray(obj, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
return Enumerable.Contains(obj, i);
add a comment |
The answer is no it doesn't optimize out the allocation of the array
Basically, every time the predicate is called, it checks against the compiler generated class and initializes a new array to call the Contains
(as you can see here)
private sealed class <>c
public static readonly <>c <>9 = new <>c();
public static Predicate<int> <>9__0_0;
internal bool <M>b__0_0(int i)
// bam!
int[] obj = new int[3];
RuntimeHelpers.InitializeArray(obj, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
return Enumerable.Contains(obj, i);
add a comment |
The answer is no it doesn't optimize out the allocation of the array
Basically, every time the predicate is called, it checks against the compiler generated class and initializes a new array to call the Contains
(as you can see here)
private sealed class <>c
public static readonly <>c <>9 = new <>c();
public static Predicate<int> <>9__0_0;
internal bool <M>b__0_0(int i)
// bam!
int[] obj = new int[3];
RuntimeHelpers.InitializeArray(obj, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
return Enumerable.Contains(obj, i);
The answer is no it doesn't optimize out the allocation of the array
Basically, every time the predicate is called, it checks against the compiler generated class and initializes a new array to call the Contains
(as you can see here)
private sealed class <>c
public static readonly <>c <>9 = new <>c();
public static Predicate<int> <>9__0_0;
internal bool <M>b__0_0(int i)
// bam!
int[] obj = new int[3];
RuntimeHelpers.InitializeArray(obj, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
return Enumerable.Contains(obj, i);
edited Apr 5 at 8:56
kara
2,30031127
2,30031127
answered Apr 5 at 8:51
Michael RandallMichael Randall
37.9k84473
37.9k84473
add a comment |
add a comment |
As @Michael Randall already wrote, it looks like it is not possible.
I agree, that your questioned code is nicely readable, having the list in the RemoveAll method. But to have the instance only once, I have three ideas of doing it:
int[] a = null;
foo.RemoveAll(i => (a ?? (a = new[] 1, 3, 5 )).Contains(i));
This is actually yours, with little un-beatuness of needing an external variable.
foo = foo.Except(new[] 1, 3, 5 ).ToList();
That's actually pretty nice solution using Linq.
new List<int>1, 3, 5.ForEach(x => foo.Remove(x));
new[] 1, 3, 5.Iterate(x => foo.Remove(x));
This is something I'd do. In neary all of my code I have my Extension method "Iterate" to avoid the need of foreach. And also, i dont want to "toList" everything all the time to make a .ForEach(..)
static class Extensions
public static void Iterate<TSource>(this IEnumerable<TSource> source, Action<TSource> action)
foreach (var item in source)
action.Invoke(item);
6
Beware,RemoveAll
performs vastly better that multiple calls toRemove
.
– Theodor Zoulias
Apr 5 at 9:42
add a comment |
As @Michael Randall already wrote, it looks like it is not possible.
I agree, that your questioned code is nicely readable, having the list in the RemoveAll method. But to have the instance only once, I have three ideas of doing it:
int[] a = null;
foo.RemoveAll(i => (a ?? (a = new[] 1, 3, 5 )).Contains(i));
This is actually yours, with little un-beatuness of needing an external variable.
foo = foo.Except(new[] 1, 3, 5 ).ToList();
That's actually pretty nice solution using Linq.
new List<int>1, 3, 5.ForEach(x => foo.Remove(x));
new[] 1, 3, 5.Iterate(x => foo.Remove(x));
This is something I'd do. In neary all of my code I have my Extension method "Iterate" to avoid the need of foreach. And also, i dont want to "toList" everything all the time to make a .ForEach(..)
static class Extensions
public static void Iterate<TSource>(this IEnumerable<TSource> source, Action<TSource> action)
foreach (var item in source)
action.Invoke(item);
6
Beware,RemoveAll
performs vastly better that multiple calls toRemove
.
– Theodor Zoulias
Apr 5 at 9:42
add a comment |
As @Michael Randall already wrote, it looks like it is not possible.
I agree, that your questioned code is nicely readable, having the list in the RemoveAll method. But to have the instance only once, I have three ideas of doing it:
int[] a = null;
foo.RemoveAll(i => (a ?? (a = new[] 1, 3, 5 )).Contains(i));
This is actually yours, with little un-beatuness of needing an external variable.
foo = foo.Except(new[] 1, 3, 5 ).ToList();
That's actually pretty nice solution using Linq.
new List<int>1, 3, 5.ForEach(x => foo.Remove(x));
new[] 1, 3, 5.Iterate(x => foo.Remove(x));
This is something I'd do. In neary all of my code I have my Extension method "Iterate" to avoid the need of foreach. And also, i dont want to "toList" everything all the time to make a .ForEach(..)
static class Extensions
public static void Iterate<TSource>(this IEnumerable<TSource> source, Action<TSource> action)
foreach (var item in source)
action.Invoke(item);
As @Michael Randall already wrote, it looks like it is not possible.
I agree, that your questioned code is nicely readable, having the list in the RemoveAll method. But to have the instance only once, I have three ideas of doing it:
int[] a = null;
foo.RemoveAll(i => (a ?? (a = new[] 1, 3, 5 )).Contains(i));
This is actually yours, with little un-beatuness of needing an external variable.
foo = foo.Except(new[] 1, 3, 5 ).ToList();
That's actually pretty nice solution using Linq.
new List<int>1, 3, 5.ForEach(x => foo.Remove(x));
new[] 1, 3, 5.Iterate(x => foo.Remove(x));
This is something I'd do. In neary all of my code I have my Extension method "Iterate" to avoid the need of foreach. And also, i dont want to "toList" everything all the time to make a .ForEach(..)
static class Extensions
public static void Iterate<TSource>(this IEnumerable<TSource> source, Action<TSource> action)
foreach (var item in source)
action.Invoke(item);
answered Apr 5 at 9:01
MaliorMalior
808311
808311
6
Beware,RemoveAll
performs vastly better that multiple calls toRemove
.
– Theodor Zoulias
Apr 5 at 9:42
add a comment |
6
Beware,RemoveAll
performs vastly better that multiple calls toRemove
.
– Theodor Zoulias
Apr 5 at 9:42
6
6
Beware,
RemoveAll
performs vastly better that multiple calls to Remove
.– Theodor Zoulias
Apr 5 at 9:42
Beware,
RemoveAll
performs vastly better that multiple calls to Remove
.– Theodor Zoulias
Apr 5 at 9:42
add a comment |
Since the compiler is not that smart, we must outsmart him.
var foo = new List<int> 1, 2, 3, 4, 5 ;
var bar = new HashSet<int>() 1, 3, 5 ;
foo.RemoveAll(i => bar.Contains(i));
1
A hash set of 3 entries will surely not perform better, it would arguably (I have not measured) perform worse.
– Sefe
Apr 5 at 12:22
3
@Sefe While there is probably a difference between the hash set and the array, I think the point of this answer was to createbar
outside of the anonymous function so that's it's created only once and to avoid the creation overhead.
– Joshua Taylor
Apr 5 at 12:42
2
@Sefe what you say is true, but I think that the OP provided a trivial example just to illustrate a point. In a real world scenario the lists will be probably longer than that.
– Theodor Zoulias
Apr 5 at 17:17
add a comment |
Since the compiler is not that smart, we must outsmart him.
var foo = new List<int> 1, 2, 3, 4, 5 ;
var bar = new HashSet<int>() 1, 3, 5 ;
foo.RemoveAll(i => bar.Contains(i));
1
A hash set of 3 entries will surely not perform better, it would arguably (I have not measured) perform worse.
– Sefe
Apr 5 at 12:22
3
@Sefe While there is probably a difference between the hash set and the array, I think the point of this answer was to createbar
outside of the anonymous function so that's it's created only once and to avoid the creation overhead.
– Joshua Taylor
Apr 5 at 12:42
2
@Sefe what you say is true, but I think that the OP provided a trivial example just to illustrate a point. In a real world scenario the lists will be probably longer than that.
– Theodor Zoulias
Apr 5 at 17:17
add a comment |
Since the compiler is not that smart, we must outsmart him.
var foo = new List<int> 1, 2, 3, 4, 5 ;
var bar = new HashSet<int>() 1, 3, 5 ;
foo.RemoveAll(i => bar.Contains(i));
Since the compiler is not that smart, we must outsmart him.
var foo = new List<int> 1, 2, 3, 4, 5 ;
var bar = new HashSet<int>() 1, 3, 5 ;
foo.RemoveAll(i => bar.Contains(i));
answered Apr 5 at 9:40
Theodor ZouliasTheodor Zoulias
37519
37519
1
A hash set of 3 entries will surely not perform better, it would arguably (I have not measured) perform worse.
– Sefe
Apr 5 at 12:22
3
@Sefe While there is probably a difference between the hash set and the array, I think the point of this answer was to createbar
outside of the anonymous function so that's it's created only once and to avoid the creation overhead.
– Joshua Taylor
Apr 5 at 12:42
2
@Sefe what you say is true, but I think that the OP provided a trivial example just to illustrate a point. In a real world scenario the lists will be probably longer than that.
– Theodor Zoulias
Apr 5 at 17:17
add a comment |
1
A hash set of 3 entries will surely not perform better, it would arguably (I have not measured) perform worse.
– Sefe
Apr 5 at 12:22
3
@Sefe While there is probably a difference between the hash set and the array, I think the point of this answer was to createbar
outside of the anonymous function so that's it's created only once and to avoid the creation overhead.
– Joshua Taylor
Apr 5 at 12:42
2
@Sefe what you say is true, but I think that the OP provided a trivial example just to illustrate a point. In a real world scenario the lists will be probably longer than that.
– Theodor Zoulias
Apr 5 at 17:17
1
1
A hash set of 3 entries will surely not perform better, it would arguably (I have not measured) perform worse.
– Sefe
Apr 5 at 12:22
A hash set of 3 entries will surely not perform better, it would arguably (I have not measured) perform worse.
– Sefe
Apr 5 at 12:22
3
3
@Sefe While there is probably a difference between the hash set and the array, I think the point of this answer was to create
bar
outside of the anonymous function so that's it's created only once and to avoid the creation overhead.– Joshua Taylor
Apr 5 at 12:42
@Sefe While there is probably a difference between the hash set and the array, I think the point of this answer was to create
bar
outside of the anonymous function so that's it's created only once and to avoid the creation overhead.– Joshua Taylor
Apr 5 at 12:42
2
2
@Sefe what you say is true, but I think that the OP provided a trivial example just to illustrate a point. In a real world scenario the lists will be probably longer than that.
– Theodor Zoulias
Apr 5 at 17:17
@Sefe what you say is true, but I think that the OP provided a trivial example just to illustrate a point. In a real world scenario the lists will be probably longer than that.
– Theodor Zoulias
Apr 5 at 17:17
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55531327%2flistt-removeall-efficiency-compiler-optimisation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
25
try for your self sharplab.io
– Michael Randall
Apr 5 at 8:33
1
@MichaelRandall Great site. You should write an answer for this with an short example. This is interesseting for everybody, i think.
– kara
Apr 5 at 8:47
1
I'm really surprised but it looks like it does actually create a new instance of the 3 item array for each iteration inside the method
internal bool <M>b__0_0(int i)
.– maxp
Apr 5 at 8:50