Недавно добавленные исходники

•  DeLiKaTeS Tetris (Тетрис)  147

•  TDictionary Custom Sort  3 328

•  Fast Watermark Sources  3 075

•  3D Designer  4 837

•  Sik Screen Capture  3 330

•  Patch Maker  3 543

•  Айболит (remote control)  3 650

•  ListBox Drag & Drop  3 004

•  Доска для игры Реверси  81 645

•  Графические эффекты  3 932

•  Рисование по маске  3 240

•  Перетаскивание изображений  2 620

•  Canvas Drawing  2 744

•  Рисование Луны  2 570

•  Поворот изображения  2 177

•  Рисование стержней  2 166

•  Paint on Shape  1 567

•  Генератор кроссвордов  2 230

•  Головоломка Paletto  1 766

•  Теорема Монжа об окружностях  2 220

•  Пазл Numbrix  1 684

•  Заборы и коммивояжеры  2 055

•  Игра HIP  1 280

•  Игра Go (Го)  1 227

•  Симулятор лифта  1 472

•  Программа укладки плитки  1 215

•  Генератор лабиринта  1 543

•  Проверка числового ввода  1 360

•  HEX View  1 494

•  Физический маятник  1 357

 
скрыть


Delphi FAQ - Часто задаваемые вопросы

| Базы данных | Графика и Игры | Интернет и Сети | Компоненты и Классы | Мультимедиа |
| ОС и Железо | Программа и Интерфейс | Рабочий стол | Синтаксис | Технологии | Файловая система |



Delphi Sources

Экономия памяти с дублирующими значениями в таблице



Оформил: DeeCo

{ 
  Often there are lots of duplicate strings in a program, 
  for example take the following database: 

  Author    Title 
  aaa        xxx 
  aaa        yyy 
  aaa        zzz 

  Suppose you want to read it into your program and 
  create one record or class per line: 
}

 while not Table.EOF do
 begin
   author := Table['Author'];
   title := Table['Title'];
   MyClass := TMyClass(author, title);
   StoreItSomewhere(MyClass);
   Table.Next;
 end;

 { 
  You end up with three different strings containting the same author. 
  No problem with that as long as there are only three of them. 
  Now suppose you have got a database of 100,000 entries and lots of 
  duplicate author names. 
  If you do the above again you will end up with a huge memory overhead 
  due to fragmentation of the free memory on the heap. 
  In addition to that freeing the objects will result in a noticable delay 
  of the program while the memory manager is busy merging free memory blocks. 
  On the other hand, Delphi's AnsiStrings use reference counting so it 
  would be possible to assign the same string to many string variables 
  without copying the contents: 
}

 Author := 'aaa';
 for i:=0 to 100000 do
 begin
   MyClass := TMyClass(Author);
   StoreItSomewhere(MyClass);
 end;

 { 
  This will create 100000 strings containting 'aaa', 
  but store the actual contents only once, 
  because they are assigned the same string constant. 

  It would be nice to do the same with the above database example, 
  wouldn't it? But how do we get Delphi to realize that 
  the content is really the same? 

  Answer: A StringList 
}

 authors := TStringList.Create;
 authors.Sorted := true;
 authors.Duplicates := dupIgnore;
 while not Table.EOF do
 begin
   author := Table['Author'];
   title := Table['Title'];

   authors.Add(author);
   authors.Search(author, Idx);
   author := authors[Idx];

   MyClass := TMyClass(author, title);
   StoreItSomewhere(MyClass);
   Table.Next;
 end;
 authors.free;

 { 
  This will only keep one string of each author's name and assign 
  it to all other occurences, even after the StringList has been freed again. 
  The wonders of reference counting! 

  I admit that the above example looks a bit odd. Nobody would load 
  such a database into memory. But I had a similar problem today and by using 
  the trick above I managed to reduce the memory usage of my program from 120 megabytes to 10 
  (basically nothing, given that it started out with 8 mb). 
}







Copyright © 2004-2024 "Delphi Sources" by BrokenByte Software. Delphi World FAQ

Группа ВКонтакте