Hi Everyone,
Does .NET offer any collection class which will give me objects last
*accessed* such that I may build a least-recently-used cache that
kills off objects that haven't been used for awhile?
Or is there any other way to implement this kind of a cache /
collection where one can do this kind of cleanup based on
least-recently-used objects?
Java has a collection class called LinkedHashSet which enables one to
do this. Is there something similar in .NET - or some other way of
doing this?
Any pointers will be really appreciated.
Thanks a ton
Regards
Vani 11 2186
Just move it to the front on access and kill those at the back. Any
collection class that allows the move and the kill will do for this, like
ArrayList.
"Vani Murarka" <va**********@g mail.com> wrote in message
news:96******** *************** **@posting.goog le.com... Hi Everyone,
Does .NET offer any collection class which will give me objects last *accessed* such that I may build a least-recently-used cache that kills off objects that haven't been used for awhile?
Or is there any other way to implement this kind of a cache / collection where one can do this kind of cleanup based on least-recently-used objects?
Java has a collection class called LinkedHashSet which enables one to do this. Is there something similar in .NET - or some other way of doing this?
Any pointers will be really appreciated.
Thanks a ton
Regards
Vani
at wrote: Just move it to the front on access and kill those at the back. Any collection class that allows the move and the kill will do for this, like ArrayList.
You may run into performance-problems, ArrayList really doesn't perform
that well on anything but appending.
You could look into using a splay-tree data-structure, it fits your
requirements pretty nice and is really easy to implement.
--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
That interests and surprises me, I have not measured the ArrayList's
performance for moving elements around but could you provide some links to
confirm your statement? I do not contradict your statement but I would like
some confirmation.
Then, an ArrayList comes standard meaning less code to write and as long as
its performance is ok why not stick with it? One can always change to
another container as the need arises and have the process itself up and
running first and then see what the performance is.
"Helge Jensen" <he**********@s log.dk> wrote in message
news:eu******** ******@TK2MSFTN GP14.phx.gbl... at wrote: Just move it to the front on access and kill those at the back. Any collection class that allows the move and the kill will do for this, like ArrayList.
You may run into performance-problems, ArrayList really doesn't perform that well on anything but appending.
You could look into using a splay-tree data-structure, it fits your requirements pretty nice and is really easy to implement.
-- Helge Jensen mailto:he****** ****@slog.dk sip:he********* *@slog.dk -=> Sebastian cover-music: http://ungdomshus.nu <=-
at wrote: That interests and surprises me, I have not measured the ArrayList's performance for moving elements around but could you provide some links to confirm your statement? I do not contradict your statement but I would like some confirmation.
Output of attached source (measured execution time since start):
00:00:00 mutation from end...
00:00:00.010014 4 mutation from end... done
00:00:00.010014 4 mutation from start...
00:00:12.377798 4 mutation from start... done
It's not really suprising, since lists implemented as arrays has to copy
the tail of the list when inserting/removing.
Then, an ArrayList comes standard meaning less code to write and as long as its performance is ok why not stick with it? One can always change to another container as the need arises and have the process itself up and running first and then see what the performance is.
Yes, the initial implementation can easily be done using ArrayList, and
if the profiler shows a performance problem there you can
re-implement.... but, I have implemented caching in the past, and
array's really aren't a good datastructure for it.
BTW: How are you going to search the cache? if it gets mederately large
you should probably have a seperate indexing on it, by hashing for example.
If my memory seves me right wrt. LinkedHashSet(J AVA), there is no way to
rearrange the ordering, moving most-used elements to the front, so it
really isn't very good for caching either.
--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
using System;
using System.Collecti ons;
namespace ArrayListPerfor mance
{
class ArrayListPerfom anceTest
{
public static void Main()
{
int count = 100000;
DateTime start = DateTime.UtcNow ;
IList l = new ArrayList();
Console.WriteLi ne("{0} mutation from end...", DateTime.UtcNow-start);
for (int i = 0; i < count; ++i)
l.Insert(i, i);
Console.WriteLi ne("{0} mutation from end... done", DateTime.UtcNow-start);
l = new ArrayList();
Console.WriteLi ne("{0} mutation from start...", DateTime.UtcNow-start);
for (int i = 0; i < count; ++i)
l.Insert(0, i);
Console.WriteLi ne("{0} mutation from start... done", DateTime.UtcNow-start);
}
}
}
I thought ArrayList was backed by a doubly linked list, I guess I was wrong.
If implemented using fixed size arrays you are completely right.
Whatever data structure, as long as it is has the operations that a doubly
linked list has (implemented as such or as some tree flavour) the one most
in front is the most recently accessed one, the next one the one accessed
before that and so on. From the other end it works the same way hence te
requirement for doybly linked list semantics.
I am not considering random access here, just access starting from head and
starting from tail and step from there.
Well, thanks anyway, for pointing out the ArrayList inefficiency.
"Helge Jensen" <he**********@s log.dk> wrote in message
news:e6******** ******@TK2MSFTN GP14.phx.gbl... at wrote: That interests and surprises me, I have not measured the ArrayList's performance for moving elements around but could you provide some links to confirm your statement? I do not contradict your statement but I would like some confirmation. Output of attached source (measured execution time since start):
00:00:00 mutation from end... 00:00:00.010014 4 mutation from end... done 00:00:00.010014 4 mutation from start... 00:00:12.377798 4 mutation from start... done
It's not really suprising, since lists implemented as arrays has to copy the tail of the list when inserting/removing.
Then, an ArrayList comes standard meaning less code to write and as long as its performance is ok why not stick with it? One can always change to another container as the need arises and have the process itself up and running first and then see what the performance is.
Yes, the initial implementation can easily be done using ArrayList, and if the profiler shows a performance problem there you can re-implement.... but, I have implemented caching in the past, and array's really aren't a good datastructure for it.
BTW: How are you going to search the cache? if it gets mederately large you should probably have a seperate indexing on it, by hashing for example.
If my memory seves me right wrt. LinkedHashSet(J AVA), there is no way to rearrange the ordering, moving most-used elements to the front, so it really isn't very good for caching either.
-- Helge Jensen mailto:he****** ****@slog.dk sip:he********* *@slog.dk -=> Sebastian cover-music: http://ungdomshus.nu <=-
--------------------------------------------------------------------------------
using System; using System.Collecti ons;
namespace ArrayListPerfor mance { class ArrayListPerfom anceTest { public static void Main() { int count = 100000; DateTime start = DateTime.UtcNow ; IList l = new ArrayList(); Console.WriteLi ne("{0} mutation from end...", DateTime.UtcNow-start); for (int i = 0; i < count; ++i) l.Insert(i, i); Console.WriteLi ne("{0} mutation from end... done", DateTime.UtcNow-start); l = new ArrayList(); Console.WriteLi ne("{0} mutation from start...", DateTime.UtcNow-start); for (int i = 0; i < count; ++i) l.Insert(0, i); Console.WriteLi ne("{0} mutation from start... done", DateTime.UtcNow-start); } } }
But, I tried the following
I measured
ArrayList al = new ArrayList();
for(int i = 0; i < 100000; i++)
{
al.Add(new TestItem(i));
}
TestItem ti;
int j = 99999;
Console.WriteLi ne("{0} starting turn around", DateTime.UtcNow );
for(int i = 0; i < 100000; i++)
{
ti = (TestItem)al[j];
al.RemoveAt(j);
al.Insert(0, ti);
j--;
}
Console.WriteLi ne("{0} turn around finished", DateTime.UtcNow );
and
public class TestItem
{
private int m;
public TestItem(int i)
{
m = i;
}
}
With the following result:
3/26/2005 4:18:20 PM starting turn around
3/26/2005 4:18:59 PM turn around finished
That is about 0.0004 seconds per move, I'd say that is better than fast
enough, at least it sufficiently fast so if moving an element to the front
is all that is needed I would initially just use an ArrayList.
Regards,
At
"at" <a@t> wrote in message news:42******** *************@n ews.xs4all.nl.. . I thought ArrayList was backed by a doubly linked list, I guess I was wrong. If implemented using fixed size arrays you are completely right.
Whatever data structure, as long as it is has the operations that a doubly linked list has (implemented as such or as some tree flavour) the one most in front is the most recently accessed one, the next one the one accessed before that and so on. From the other end it works the same way hence te requirement for doybly linked list semantics.
I am not considering random access here, just access starting from head and starting from tail and step from there.
Well, thanks anyway, for pointing out the ArrayList inefficiency.
"Helge Jensen" <he**********@s log.dk> wrote in message news:e6******** ******@TK2MSFTN GP14.phx.gbl... at wrote: That interests and surprises me, I have not measured the ArrayList's performance for moving elements around but could you provide some links to confirm your statement? I do not contradict your statement but I would like some confirmation.
Output of attached source (measured execution time since start):
00:00:00 mutation from end... 00:00:00.010014 4 mutation from end... done 00:00:00.010014 4 mutation from start... 00:00:12.377798 4 mutation from start... done
It's not really suprising, since lists implemented as arrays has to copy the tail of the list when inserting/removing.
Then, an ArrayList comes standard meaning less code to write and as long as its performance is ok why not stick with it? One can always change to another container as the need arises and have the process itself up and running first and then see what the performance is.
Yes, the initial implementation can easily be done using ArrayList, and if the profiler shows a performance problem there you can re-implement.... but, I have implemented caching in the past, and array's really aren't a good datastructure for it.
BTW: How are you going to search the cache? if it gets mederately large you should probably have a seperate indexing on it, by hashing for example.
If my memory seves me right wrt. LinkedHashSet(J AVA), there is no way to rearrange the ordering, moving most-used elements to the front, so it really isn't very good for caching either.
-- Helge Jensen mailto:he****** ****@slog.dk sip:he********* *@slog.dk -=> Sebastian cover-music: http://ungdomshus.nu <=-
--------------------------------------------------------------------------------
using System; using System.Collecti ons;
namespace ArrayListPerfor mance { class ArrayListPerfom anceTest { public static void Main() { int count = 100000; DateTime start = DateTime.UtcNow ; IList l = new ArrayList(); Console.WriteLi ne("{0} mutation from end...", DateTime.UtcNow-start); for (int i = 0; i < count; ++i) l.Insert(i, i); Console.WriteLi ne("{0} mutation from end... done", DateTime.UtcNow-start); l = new ArrayList(); Console.WriteLi ne("{0} mutation from start...", DateTime.UtcNow-start); for (int i = 0; i < count; ++i) l.Insert(0, i); Console.WriteLi ne("{0} mutation from start... done", DateTime.UtcNow-start); } } }
at wrote: I thought ArrayList was backed by a doubly linked list, I guess I was wrong. If implemented using fixed size arrays you are completely right.
It's backed by an Array :)
Based on experimental evidence, the Array is reallocated when full. I'm
guessing it uses reallocation by multiplying the current size (O(n)
amortized for n inserts).
I am not considering random access here, just access starting from head and starting from tail and step from there.
Didn't you mention a cache? what do you do lookup based on?
Well, thanks anyway, for pointing out the ArrayList inefficiency.
Well, it may or may not be a problem... atleast you know why it could be
slow.
--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
at wrote: That is about 0.0004 seconds per move, I'd say that is better than fast enough, at least it sufficiently fast so if moving an element to the front is all that is needed I would initially just use an ArrayList.
Good for you.
The expected expense of a randomly remove/insert would be O((n/2(^2)).
If the cache is smaller than 10k this may be an acceptible delay for
you, especially if the cached calculation is very expensive or lookups
are infrequent.
Of course you can always change to a "better" implementation later.
--
Helge Jensen
mailto:he****** ****@slog.dk
sip:he********* *@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
If the important element is expiration of older items, rather than
sorting by access, you can use the Cache object from
System.Web.Cach ing. It supports timed expirations, both fixed and
sliding, as well as callbacks fired on removed items.
I don't know what the associated overhead is, but I hope that helps.
Good luck.
~ Jeff
Vani Murarka wrote: Hi Everyone,
Does .NET offer any collection class which will give me objects last *accessed* such that I may build a least-recently-used cache that kills off objects that haven't been used for awhile?
Or is there any other way to implement this kind of a cache / collection where one can do this kind of cleanup based on least-recently-used objects?
Java has a collection class called LinkedHashSet which enables one to do this. Is there something similar in .NET - or some other way of doing this?
Any pointers will be really appreciated.
Thanks a ton
Regards
Vani This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: GrumpyDev |
last post by:
what is the best way to implement collection
of custom entities?
|
by: Scott |
last post by:
I have a collection where the items in the collection are dates. I want to
iterate over the collection and build a value list string for the rowsource
of a listbox. The dates in the collection are not in chronological order. Is
there a way to first sort the collection and put the dates in chronological
order before creating the value list string? Or, how would I iterate over
the collection pulling out the dates in chronological order?
...
|
by: Dan H. |
last post by:
Hello,
I have an application that requires a collection that is sorted by a double
field first, and then by a long field.
So, lets say I have an class that has 2 fields called time, priority. I
want to put a bunch of those classes into a collection and have that
collection always stay sorted by time first, then priority. The last object
in the list should be the lowest time, highest priority object, etc, for
easy picking.
|
by: Mark |
last post by:
Assume you have a strongly typed collection of a class called Person. The
strongly typed collection implements IEnumerable so it can be databound to a
server control like a DataGrid. The Person class has several public
properties like FirstName, LastName, and Gender.
What steps would it take to allow the collection to be sorted in multiple
ways when bound to a DataGrid? In the past, I used the sort property of a
DataView containing a...
|
by: Mel |
last post by:
I have a large collection of custom objects, each representing a period in
time with each having a start datetime and an end datetime. I frequently
need to query this collection to return a subset of the objects that fall
completely or partially between two specified dates. The way I'm doing this
at the moment is to iterate thru the entire collection on each query and
pull out the valid objects, but this is hardly an optimal way to do it....
| |
by: Michael K. Walter |
last post by:
I'd like to create a strongly-typed collection of objects that are indexed
by a string value (key is a string). I'd also like to allow users to iterate
over the collection using a For-each loop where an object of the contained
type is returned (instead of just the standard DictionaryEntry object).
- only allow objects of type MyObj to be added to the collection
- return a MyObj type in the For-each loop
What's the best way to do this?
|
by: Vani Murarka |
last post by:
Hi Everyone,
Does .NET offer any collection class which will give me objects last
*accessed* such that I may build a least-recently-used cache that
kills off objects that haven't been used for awhile?
Or is there any other way to implement this kind of a cache /
collection where one can do this kind of cleanup based on
least-recently-used objects?
|
by: Burt |
last post by:
I need to create a collection of classes (or structures) can be
accessed by a string key, eg MyColl("ShortName5").Name for class with
key ShortName5. But it also has to be sorted by a second ordering
field.
Hashtable offers access by key, but is not sorted. Arraylist is sorted
(the fields will come presorted from the db), but I can't access by
key. SortedList can't be sorted by a field different from the key
field.
|
by: jwilson128 |
last post by:
I am looking for some help on the best type of collection to use and/
or best way to implement the collection with the following
functionality:
I have defined an object to represent an interest rate:
Public Class IRFcast
Public CloseDate As Date
Public RateIndex As String
Public RateClose As Double
End Class
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |