.net - ArrayList sort method -
a friend , writing software (as side project) , having trouble arraylists.
we're storing collection of instances of custom object (containing datetime , 2 strings) in arraylist. once we've stored of entries in arraylist, we're sorting them datetime. problem is, we're having store of 100,000 instances of object, means built in sort method taking long time - clocked @ on hour, @ 1 point.
the speed of sort isn't of issue, wondering whether there better way sort elements in arraylist using built in sort method. although guess not, based on fact built in .net stuff highly optimised.
note: we're using arraylists because of middleware we've chosen use generating pdf reports, based on content of arraylist. guess, if had chance move on list<> sort methods better. or they?
edit:
based on requests source code, i'll post some. i'm not sure how can provide isn't obvious.
public class dataobject : icompareable { private datetime timestamp; private string description; private string detail; public dataobject (datatime intimestamp, string indescription, string indetail) { this.timestamp = intimestamp; this.description = indescription; this.detail = indetail; } int icomparable.compareto(object that) { dataobject mythat = (dataobject)that; return this._timestamp.compareto(mythat._timestamp); } } // .... // arraylist datalist = new arraylist(); (int = 0; < database.packets.count; i++) { datalist.add(new dataobject(database.packet(i).gettimestamp(), database.packet(i).getdescription(), database.packet(i).getdetail()); } // ... same above, other data // ... types (all parse strings when pulled // ... database datalist.sort(); that's it, largely. we're pulling data several places in sqlceme3.5 database (we're using .net 3.5, can't use linq), placing them in arraylist of objects , using object arraylist further down pipe.
we want records multiple places in database (some packets, strings (prompts), other types, parse down strings) , sort them time stamp. want data interspersed - packet followed string value, followed object value, if that's order stored/raised in.
we have read-only access database, don't think using database, sort them idea (or possible). being said, new sql - never used before project. can done?
either problem somewhere else, or code posted isn't accurate representation of what's running. or, perhaps, data in bad order causes sort exhibit worst case behavior. find last pretty unlikely.
here's test program adds 100,000 of dataobject instances arraylist , calls sort. executes in less 50 milliseconds on machine.
note .net 4.5, not 3.5. however, can't imagine sort horribly broken in earlier version.
public class dataobject : icomparable { private datetime timestamp; private string description; private string detail; public dataobject(datetime intimestamp, string indescription, string indetail) { this.timestamp = intimestamp; this.description = indescription; this.detail = indetail; } public int compareto(object that) { dataobject mythat = (dataobject)that; return this.timestamp.compareto(mythat.timestamp); } } public class program { private static void main(string[] args) { // create arraylist dataobject items. const int numitems = 100000; // items random time stamps within last year datetime enddate = datetime.now; datetime basedate = enddate.addyears(-1); int secondsrange = (int)((enddate - basedate).totalseconds); random rnd = new random(); console.writeline("adding {0} items list.", numitems); arraylist datalist = new arraylist(); (int = 0; < numitems; ++i) { datetime ts = basedate.addseconds(rnd.next()); dataobject item = new dataobject(ts, "foo", "bar"); datalist.add(item); } console.write("sorting list..."); stopwatch sw = stopwatch.startnew(); datalist.sort(); sw.stop(); console.writeline("done!"); console.writeline("elapsed time {0} ms", sw.elapsedmilliseconds); console.readline(); } }
Comments
Post a Comment