Author: Adrien Regimbald
Date: 23:28:27 06/09/00
Go up one level in this thread
Hello, >I would like to not only second this, but add a strange anecdote. > >I once wrote a sorting program that added strings into a linked list. >I allocated memory for the strings as I read them and added them to the list. >The malloc() profiler time was unbelievable. > >I tried a different approach. I read the whole file first, and then did the >total allocation, and then read the file again. > >Reading the file twice was FASTER than individual malloc calls. I couldn't >believe it. > >That was a long time ago (I seem to recall it was under OS/2 but that is not for >sure). Anyway, malloc() and new can be incredibly costly. I don't find this strange or unbelievable in any way :P There is a certain overhead to using malloc () .. if you allocate only once, you are saving your computer a LOT of work.. there are some tradeoffs to consider - when does the price of reading the file twice offset the cost of the multiple malloc statements, and how much are you willing to sacrifice in terms of writing clean code to gain a few seconds? ;P You also have to consider that on some systems, reading the file twice might be MUUUUUUUUUUUUUUUUUUUUUUUUCH slower due to slow access to the hard drive. I typically try to avoid these types of issues, and go with the method that is "cleaner" :) Regards, Adrien.
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.