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

•  DeLiKaTeS Tetris (Тетрис)  1 563

•  TDictionary Custom Sort  4 217

•  Fast Watermark Sources  3 891

•  3D Designer  6 010

•  Sik Screen Capture  4 160

•  Patch Maker  4 698

•  Айболит (remote control)  4 611

•  ListBox Drag & Drop  3 758

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

•  Графические эффекты  4 948

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

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

•  Canvas Drawing  3 638

•  Рисование Луны  3 431

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

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

•  Paint on Shape  1 990

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

•  Головоломка Paletto  2 177

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

•  Пазл Numbrix  1 954

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

•  Игра HIP  1 569

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

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

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

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

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

•  HEX View  1 829

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

 
скрыть


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

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