开发者

fast retrieval of (void *) memory block

I have a system that returns a void* to a memory block. This memory block stores contiguous data records of different types(int,char,double etc.) and gives the number of bytes of each field in each record.I essentially look up the type of the record and g开发者_JAVA百科et the value of the record. To retrieve all the records, I do

switch(type)

{

   case 'int' : \*(int*)(ptr+index)

   case 'char': \*(char*)(ptr+index)

}

When I have to go through 300000 records this is taking a lot of time.Is there a faster way to go through all the records?


If a single block can be of multiple types that can only be resolved at runtime, you will have to dispatch to handlers in a switch statement. Note that:

  1. unions are usually used in C for such things to save space
  2. switch statements are very fast and translate to constant-time lookup tables


If I understand your question correctly -- I assume you're going through each record, sortof saying "for each record i, if its type is 'char' then access the record at location i".

If you know how many records you have to access in advance, can't you just cache them all first?

If I'm completely off track forgive me for not understanding your point.


Your comments finally gave enough information to answer the question: you're writing to an ostringstream. Which means that you're probably doing a lot of string manipulation inside your switch. Figure out how to optimize this, and your performance problems should go away.

(to convince yourself this is the case, simply comment out all code that references the stream and run your program again)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜