tag:blogger.com,1999:blog-4434108347727659251.post7638289197274064202..comments2022-06-29T08:53:03.580+02:00Comments on Compas Pascal: When to use record instead of classesUnknownnoreply@blogger.comBlogger12125tag:blogger.com,1999:blog-4434108347727659251.post-50081063247139505522009-12-16T09:36:37.408+01:002009-12-16T09:36:37.408+01:00Do keep in mind though, that because a record isn&...Do keep in mind though, that because a record isn't a class, if you for instance have a function that returns a record from an array - you will not get a pointer of that record, as you would get to a class.<br /><br />A := FuncToGetRec(21);<br />SetRecValue(21, 'x');<br />// A.Value is still the old valueAnonymoushttps://www.blogger.com/profile/11793263778565246054noreply@blogger.comtag:blogger.com,1999:blog-4434108347727659251.post-78884446807942907042009-12-12T11:49:57.124+01:002009-12-12T11:49:57.124+01:00Records usually created on the stack (as in origin...Records usually created on the stack (as in original post). Classes always created on the heap (i.e. in memory which allocated dynamically).<br /><br />Records has no memory overhead, classes added 8b per instance (as noted by Krystian).<br /><br />Creating non managed records is free. Classes do a lot of job in 'background' (clear memory, calling InitInstance, AfterConstruction etc. <a Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4434108347727659251.post-49058098444703257162009-12-12T09:22:50.184+01:002009-12-12T09:22:50.184+01:00@Krystian: That is not a bug, it is fully intentio...@Krystian: That is not a bug, it is fully intentional. The purpose was to maximize speed by using array of record, and not array of pointer to record.Lars Dhttps://www.blogger.com/profile/06217303713945608384noreply@blogger.comtag:blogger.com,1999:blog-4434108347727659251.post-70814578039806943422009-12-11T22:47:52.602+01:002009-12-11T22:47:52.602+01:00good to know.
there are something to note:
1. yo...good to know. <br /><br />there are something to note:<br />1. you can consider a record as an array for different data types.<br />2. you get a free clone feature by using a record automatically (in compare with a class)<br />3. your demo code looks ugly... (begin not in next line) ;-)stanleyxu (2nd)https://www.blogger.com/profile/12291786770395325696noreply@blogger.comtag:blogger.com,1999:blog-4434108347727659251.post-52357769172493939412009-12-11T20:35:12.418+01:002009-12-11T20:35:12.418+01:00There is a 'bug' in this case test. You ar...There is a 'bug' in this case test. You are comparing time of creating classes + fill data, with filling records (no record create involved!).<br /><br />In this benchmark, array of record (memory block of 1000000*3*SizeOf(Integer) bytes) are created as one single block of memory.<br /><br />It should be benchamrked as below (creating + filling records):<br /><br />type<br /> Krystianhttps://www.blogger.com/profile/01677350991067490069noreply@blogger.comtag:blogger.com,1999:blog-4434108347727659251.post-2417480678052199762009-12-11T10:34:38.526+01:002009-12-11T10:34:38.526+01:00Cool that you've blogged about it.
Some people...Cool that you've blogged about it.<br />Some people do not understand that NOT every damn thing must be an object. <br /><br />This hurts performance, and hurts badly especialy in algorithmic problems.Soul Intruderhttps://www.blogger.com/profile/16873694208138872553noreply@blogger.comtag:blogger.com,1999:blog-4434108347727659251.post-83919894181398911672009-12-10T16:27:46.605+01:002009-12-10T16:27:46.605+01:00You conclude that Strings in records make the reco...You conclude that Strings in records make the record slower. I think you can avoid this by setting the string length (i.e. s:String[255];). Long strings are also allocated dynamically...Jørn E. Angeltveithttps://www.blogger.com/profile/17894986324482699893noreply@blogger.comtag:blogger.com,1999:blog-4434108347727659251.post-25015002100982479042009-12-10T15:25:17.953+01:002009-12-10T15:25:17.953+01:00I made these benchmarks, because I wanted to know ...I made these benchmarks, because I wanted to know the actual difference in speed more precisely, for a specific performance optimization that I was considering. I did not make other benchmarks.Lars Dhttps://www.blogger.com/profile/06217303713945608384noreply@blogger.comtag:blogger.com,1999:blog-4434108347727659251.post-86808014065587671572009-12-10T15:14:59.941+01:002009-12-10T15:14:59.941+01:00Very useful article, thank you.
Have you tried an...Very useful article, thank you.<br /><br />Have you tried any other data types in the class / record?Anonymoushttps://www.blogger.com/profile/01942427476351285299noreply@blogger.comtag:blogger.com,1999:blog-4434108347727659251.post-54739959232375071002009-12-10T13:32:19.970+01:002009-12-10T13:32:19.970+01:00It's actually not that surprising. The key di...It's actually not that surprising. The key difference between records and classes is the memory management. (Beside the fact that records can't take advantage of OO techniques like inheritance, polymorphism etc)<br /><br />The record type variables uses the local memory - the stack, whereas the class type variables needs to allocate memory dynamically on the heap.<br /><br />Have you Jørn E. Angeltveithttps://www.blogger.com/profile/17894986324482699893noreply@blogger.comtag:blogger.com,1999:blog-4434108347727659251.post-41943139378344727532009-12-10T12:52:59.625+01:002009-12-10T12:52:59.625+01:00I like to use records rather than classes in situa...I like to use records rather than classes in situations where I'm storing only simple types and I don't want to be bothered with calling constructors and destructors.<br /><br />The new Delphi 2010 TRttiContext is a bit like this. It's a record so you don't have to worry about freeing it after you're done with it.LachlanGhttps://www.blogger.com/profile/05666168623106775966noreply@blogger.comtag:blogger.com,1999:blog-4434108347727659251.post-32440867942860667442009-12-10T11:42:33.801+01:002009-12-10T11:42:33.801+01:00interesting
thank youinteresting<br /><br />thank youAlinhttps://www.blogger.com/profile/16282921205773101352noreply@blogger.com