Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / January 2005

Tip: Looking for answers? Try searching our database.

Managed function vs unmanaged function

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Z-Mechanic - 25 Jan 2005 08:21 GMT
Hello everybody!

I have a question. Anybody knows why managed function calls faster in MC++
rather than unmanaged function (defined with #pragma umanaged)? The
differences is about 4 times slower.

Thanks for your answers!
Z-Mechanic.
Kapil Khosla [MSFT] - 25 Jan 2005 15:35 GMT
Could you send us some code with compiler switches you used so that we can
do performance comparisons ?
For eg, is the function you are trying to call virtual ? Is it called from a
managed callsite ? Did you compile it pure?
Thanks,
Kapil

> Hello everybody!
>
[quoted text clipped - 4 lines]
> Thanks for your answers!
> Z-Mechanic.
Z-Mechanic - 26 Jan 2005 10:05 GMT
Hi!

I have a math library that makes 3D matrices (4x4) multiplication. After
compillation it placing in same assembly. When I compile that source with
#pragma unmanaged it works 2 times faster rather tham managed. It's fine.

But... Simple test. Source code of test files listed below. Simply call
TestCPP::Class1::Test(0);

---- Begin of TestCPP.h ----
// TestCPP.h
#pragma once

using namespace System;

int vvv(int i);

int vvv1(int i);
int vvv2(int i);
int vvv3(int i);
int vvv4(int i);
int vvv5(int i);

namespace TestCPP
{
    public __gc class Class1
    {
    public:   
        static void Test(int v)
        {
            int m = 0;

            for (int j = 0; j < 100; j++)
                for (int i = 0; i < 1000000; i++)
                {
                    m = vvv( i );
//                    m = vvv1( i );
//                    m = vvv2( m );
//                    m = vvv3( m );
//                    m = vvv4( m );
//                    m = vvv5( m );
                }

            m++;
        }
    };
}
---- End of TestCPP.h ----

---- Begin of TestCPP.cpp ----
// This is the main DLL file.

#include "stdafx.h"

#include "TestCPP.h"

#pragma unmanaged

int vvv(int i)
{
    i = vvv1( i );
    i = vvv2( i );
    i = vvv3( i );
    i = vvv4( i );
    i = vvv5( i );

    return i;
}

int vvv1(int i)
{
    i++;
    return i;
}

int vvv2(int i)
{
    i++;
    return i;
}

int vvv3(int i)
{
    i++;
    return i;
}

int vvv4(int i)
{
    i++;
    return i;
}

int vvv5(int i)
{
    i++;
    return i;
}
#pragma managed
---- End of TestCPP.cpp ----

------------------------------------------------------------------------------------------------

It makes the same. But when you uncomment calls vvv1, vvv2, vvv3, vvv4, vvv5
and comment vvv you will get execution much longer that present now.
Steve McLellan - 25 Jan 2005 19:40 GMT
It can be down to managed / unmanaged transitions, depend on what's going on
inside the function, and lots of other factors. If you could post the
function along with its calling context, someone will be able to give you a
definitive explanation. We didn't actually find a significant performance
hit when we eradicated all the "#pragma unmanaged" directives from our maths
code, and we found that performance could be greatly affected by unnecessary
managed/unmanaged transitions. Unless you've got a really good reason to use
#pragma unmanaged, and unless that chunk of code is fairly large and doesn't
call back into managed code, you might be better leaving it all as managed
code.

Steve

> Hello everybody!
>
[quoted text clipped - 4 lines]
> Thanks for your answers!
> Z-Mechanic.
Z-Mechanic - 26 Jan 2005 10:05 GMT
Hi!

I have a math library that makes 3D matrices (4x4) multiplication. After
compillation it placing in same assembly. When I compile that source with
#pragma unmanaged it works 2 times faster rather tham managed. It's fine.

But... Simple test. Source code of test files listed below. Simply call
TestCPP::Class1::Test(0);

---- Begin of TestCPP.h ----
// TestCPP.h
#pragma once

using namespace System;

int vvv(int i);

int vvv1(int i);
int vvv2(int i);
int vvv3(int i);
int vvv4(int i);
int vvv5(int i);

namespace TestCPP
{
    public __gc class Class1
    {
    public:   
        static void Test(int v)
        {
            int m = 0;

            for (int j = 0; j < 100; j++)
                for (int i = 0; i < 1000000; i++)
                {
                    m = vvv( i );
//                    m = vvv1( i );
//                    m = vvv2( m );
//                    m = vvv3( m );
//                    m = vvv4( m );
//                    m = vvv5( m );
                }

            m++;
        }
    };
}
---- End of TestCPP.h ----

---- Begin of TestCPP.cpp ----
// This is the main DLL file.

#include "stdafx.h"

#include "TestCPP.h"

#pragma unmanaged

int vvv(int i)
{
    i = vvv1( i );
    i = vvv2( i );
    i = vvv3( i );
    i = vvv4( i );
    i = vvv5( i );

    return i;
}

int vvv1(int i)
{
    i++;
    return i;
}

int vvv2(int i)
{
    i++;
    return i;
}

int vvv3(int i)
{
    i++;
    return i;
}

int vvv4(int i)
{
    i++;
    return i;
}

int vvv5(int i)
{
    i++;
    return i;
}
#pragma managed
---- End of TestCPP.cpp ----

------------------------------------------------------------------------------------------------

It makes the same. But when you uncomment calls vvv1, vvv2, vvv3, vvv4, vvv5
and comment vvv you will get execution much longer that present now.
Steve McLellan - 26 Jan 2005 18:33 GMT
This is related to exactly what I said; in your managed call, you go from
calling one unmanaged function to calling 5. When they're wrapped into a
single unmanaged function in the DLL, there's less managed -> unmanaged
transitions, and less DLL overheads. There was a nother thread on here a few
days ago about all this, and there's lots of info on the net.
I can't see much advantage making this unmanaged, to be honest - I doubt
you'll see a big performance increase.

Steve

> Hi!
>
[quoted text clipped - 102 lines]
> vvv5
> and comment vvv you will get execution much longer that present now.
Z-Mechanic - 27 Jan 2005 06:39 GMT
Hi!

Greate thanks for explanations!

It was a test only to check perfomance on procedure calling. As I said
before I got a great increase of perfomance on a complex math functions when
I put my code to unmanaged.
Steve McLellan - 27 Jan 2005 11:20 GMT
Hi,

Yes, typically with complex instructions you will find performance
increases. It's just important to ensure that you're not constantly
transitioning between managed and unmanaged code, or you'll lose the
benefits. I'm sorry I can't find the explanation of all this; if you Google
for something like "Unmanaged managed code transition" you'll probably get
some info.

Steve

> Hi!
>
[quoted text clipped - 4 lines]
> when
> I put my code to unmanaged.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.