473,396 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

unsafe loop not as fast as id like

Hi, I have a binary sorted tree wich I find my app is spending most of its time in
so I decided to try speed it up a bit ...

I tested converting it from classes to an indexed array of structs so I could use
unsafe pointers, however although it is definatly faster it only manages
about 90 million iterations per second, wich doesnt seem a lot
on my AMD64 3200+ cpu

I have played around with the data size, and made sure ive kept
it within the cpu cache, as it goes a lot slower if I increase
the data size above about 1Mb.

this is just a test, so the tree simply has nodes of 3 ints,
one for the value, and a high and low index.

using randomised data and no tree balancing its slower than the dictionary class,
but with balancing should improve marginaly.
however dictionary class requires a key and value,
and doesnt allow duplicate entries so needs 2 operations and handling for duplicates
so I assumed there was some room for improvment.

public int Find(int hash)
{
int index = 0;
searchItemCnt++;
unsafe
{
fixed (IndexedBinaryTreeNode* nodeArrayPtr = &NodeArray[0])
{
while (true)
{
IndexedBinaryTreeNode* node = nodeArrayPtr + index;
searchNodeCnt++;
if (hash < node->intValue)
{
if (node->_LowerNode < 0)
return -1;
index = node->_LowerNode;
}
else if (hash node->intValue)
{
if (node->_HigherNode < 0)
return -1;
index = node->_HigherNode;
}
else
{
return index;
}
}
}
}
}

I thought unsafe code would go faster ..

is there any other options I should know about ?
ive turned off overflow check and run it in release from
outside the debugger.

other than that I may consider using a lower level language,
although I imagine there may well already be such implementations available.
or am I expecting too much?

thanks
Colin
Oct 16 '08 #1
0 986

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: pertheli | last post by:
Hello, I have a large array of pointer to some object. I have to run test such that every possible pair in the array is tested. eg. if A,B,C,D are items of the array, possible pairs are AB, AC,...
7
by: Daniel Vallstrom | last post by:
I am having trouble with floating point addition because of the limited accuracy of floating types. Consider e.g. x0 += f(n) where x0 and f are of some floating type. Sometimes x0 is much larger...
7
by: _ed_ | last post by:
I'd like to build a class or struct composed of pointers to variables. Does this require dropping into an 'unsafe' block, or is there a trick? .... int value1 = 1234; bool value2 = false;...
0
by: AnthonyBenbrook | last post by:
Hello I'm writing some code to perform image manipulation via GDI+. In order to do this efficiently, I've set up the normal flow for accessing pixel data in a Bitmap object: BitmapData data...
19
by: vamshi | last post by:
Hi all, This is a question about the efficiency of the code. a :- int i; for( i = 0; i < 20; i++ ) printf("%d",i); b:- int i = 10;
0
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I have two piece of unsafe code. In the first one I'm getting the byte* pointer of a Bitmap data, and the second one in inside a loop that uses that byte* pointer to set the data in the Bitmap. ...
26
by: a.mil | last post by:
I am programming for code-speed, not for ansi or other nice-guy stuff and I encountered the following problem: When I have a for loop like this: b=b0; for (a=0,i=0;i<100;i++,b--) { if (b%i)...
5
by: james | last post by:
Hey Guys, Would anyone mind explaining why a foreach will implicitly do unsafe casts, and if there is a way to turn this off? Here is an example: ulong vals = new ulong { ulong.MaxValue };...
8
by: SaltyBoat | last post by:
Needing to import and parse data from a large PDF file into an Access 2002 table: I start by converted the PDF file to a html file. Then I read this html text file, line by line, into a table...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.