The implementation of a queue class (70 times faster than the built-in speed of Delphi)

Source: Internet
Author: User

Test Results

Implemented code

 

Unit sfcontnrs;

Interface

{$ Define multi_thread_queue} // thread security version. If thread security is not required, comment out this line of code.

{$ Ifdef multi_thread_queue}
Uses
Windows;
{$ Endif}

Type
Tsfqueue = Class
Private
Fcapacity: integer;
Ftmpbuff: pointer;
Fbuff: pointer;
Fposition: integer;
{$ Ifdef multi_thread_queue}
FCS: trtlcriticalsection;
{$ Endif}
//\\
Fpushindex: integer;
Fpopindex: integer;

Procedure lock ();
Procedure unlock ();
Procedure inernal_setcapacity (const value: integer );
//\\
Procedure setcapacity (const value: integer );
Function getcapacity: integer;
Public
Constructor create (initcapacity: integer = 1024 );
Destructor destroy (); override;
//\\
Function push (aitem: pointer): pointer;
Function POP (): pointer;
Public
Property capacity: integer read getcapacity write setcapacity;
End;

Implementation

{Tsfqueue}

Constructor tsfqueue. Create (initcapacity: integer );
Begin
{$ Ifdef multi_thread_queue}
Initializecriticalsection (FCS );
{$ Endif}

If initcapacity <1024 then initcapacity: = 1024;

Inernal_setcapacity (initcapacity );

End;

Destructor tsfqueue. Destroy;
Begin
Freemem (fbuff );
If ftmpbuff <> nil then
Freemem (ftmpbuff );
//\\
{$ Ifdef multi_thread_queue}
Deletecriticalsection (FCS );
{$ Endif}

Inherited;
End;

Procedure tsfqueue. lock;
Begin
{$ Ifdef multi_thread_queue}
Entercriticalsection (FCS );
{$ Endif}
End;

Procedure tsfqueue. Unlock;
Begin
{$ Ifdef multi_thread_queue}
Leavecriticalsection (FCS );
{$ Endif}
End;

Procedure tsfqueue. inernal_setcapacity (const value: integer );
VaR
Pagecount, asize: integer;
Begin
If value> fcapacity then
Begin
If ftmpbuff <> nil then
Freemem (ftmpbuff );

// Resize
Asize: = value * 4; // calculate the number of bytes required.
Pagecount: = asize Div 4096;
If (asize mod 4096)> 0 then Inc (pagecount );

// Transfer Data
Getmem (ftmpbuff, pagecount * 4096 );
Fillchar (ftmpbuff ^, pagecount * 4096, #0 );

If fbuff <> nil then
Begin
Move (fbuff ^, ftmpbuff ^, fcapacity * 4 );
Freemem (fbuff );
End;

Fbuff: = ftmpbuff;

// Calculate the new capacity
Fcapacity: = (pagecount * 4096) Div 4;

If fcapacity> = 2048 then
Begin
// Ftmpbuff allocation is used for pop, and the mobile memory is used
Getmem (ftmpbuff, pagecount * 4096 );
End
Else
Ftmpbuff: = nil;
End;
End;

 

Function tsfqueue. Pop: pointer;
Procedure adjuestmem ();
VaR
Psrc: pinteger;
PTMP: pointer;
Begin
Fillchar (ftmpbuff ^, fcapacity * 4, #0 );
Psrc: = pinteger (fbuff );
INC (psrc, fpopindex );
Move (psrc ^, ftmpbuff ^, (fcapacity-fpopindex) * 4 );
//\\
// Exchange pointer
PTMP: = fbuff;
Fbuff: = ftmpbuff;
Ftmpbuff: = pTMP;
//\\
End;

Const
_ Moverange _ = 2048;

VaR
P: pinteger;
Begin
Lock ();
Try
Result: = nil;
If (fpopindex = fpushindex) then
Exit;
P: = pinteger (fbuff );
INC (p, fpopindex );
Result: = pointer (P ^ );
INC (fpopindex );
// The idle memory at the bottom of the queue reaches 8192 Of the total relocation
If fpopindex = _ moverange _ then
Begin
Adjuestmem ();
Fpopindex: = 0;
Dec (fpushindex, _ moverange _);
End;
Finally
Unlock ();
End;
End;

Function tsfqueue. Push (aitem: pointer): pointer;
VaR
P: pinteger;
Begin
Lock ();
Try
P: = pinteger (fbuff );
INC (p, fpushindex );
P ^: = INTEGER (aitem );
INC (fpushindex );
If fpushindex> = fcapacity then
Begin
// Add up to 1024 locations for expansion
Inernal_setcapacity (fcapacity + 1024 );
End;
Finally
Unlock ();
End;
End;

Procedure tsfqueue. setcapacity (const value: integer );
Begin
Lock ();
Try
Inernal_setcapacity (value );
Finally
Unlock ();
End;
End;

Function tsfqueue. getcapacity: integer;
Begin
Lock ();
Try
Result: = self. fcapacity;
Finally
Unlock ();
End;
End;

End.

 

 

 

// Test the Function

// SW high-precision time

Procedure tfrmmain. btnqueueclick (Sender: tobject );
VaR
A: tsfqueue; // high-speed team implementation after optimization (thread security)
B: tqueue;
Index: integer;
Begin
A: = tsfqueue. Create ();
B: = tqueue. Create ();
Sw. Start ();
For indexes: = 1 to 10000*2 do
Begin
B. Push (0 );
End;
For indexes: = 1 to 10000*2 do
Begin
B. Pop ();
End;

Sw. Stop ();

Showmessage (inttostr (SW. elapsedmiliseconds ));

End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.