Re: Roll your own std::vector ???
- From: "Peter Olcott" <NoSpam@xxxxxxxxxxxxx>
- Date: Fri, 22 Dec 2006 09:45:45 -0600
"Joanna Carter [TeamB]" <joanna@xxxxxxxxxxxx> wrote in message
news:%23xRh1adJHHA.2312@xxxxxxxxxxxxxxxxxxxxxxx
"Peter Olcott" <NoSpam@xxxxxxxxxxxxx> a écrit dans le message de news:
8kRih.128707$xM4.49449@xxxxxxxxxxxxxxx
| > If you call (for instance) myInt.ToString() then boxing isn't involved.
My apologies, I assumed boxing occurred here.
| It would still seem like far less than the best possible design of all
possible
| designs. It still seems like run-time overhead that could have been done
at
| compile-time.
Rathermore, it is usually down to developer laziness, using object
parameters to methods where strictly typed methods would be more efficient
:-)
Many times, developers, wanting to do the same thing to different types,
will use the following method signature :
string ConvertToString(object value)
{
return value.ToString();
}
Now, this means that any type may be passed to the method, but that boxing
will occur. However if, in .NET 1.1, we use overloaded method signatures :
string ConvertToString(int value)
{
return value.ToString();
}
string ConvertToString(double value)
{
return value.ToString();
}
void Test()
{
int i = 123;
string s = ConvertToString(i);
}
... the correct overloaded method will be called for the true type of the
parameter and the native version of ToString() for each type will be called.
Or you could use generics to simplify this even further :
string ConvertToString<T>(T value)
{
return value.ToString();
}
void Test()
{
int i = 123;
string s = ConvertToString(i);
}
The compîler infers the type of the generic method from the type being
passed to the value parameter. A true int gets passed to the method and
ToString() gets called on the native type with no boxing.
So, you can see, for a little more effort on the part of the develpoer,
boxing can be avoided again.
Joanna
This would now seem to make much more sense. As long as the unnecessary overhead
can be easily avoided, and the purpose of this overhead is to make it a little
easier on the programmer for applications where performance is not important,
then there would be no problem. Now that Generics exist, the fast way is also
just as easy. I was pleasantly surprised to find that managed code in 2005
seemed to perform better that native code under Visual C++ 6.0 on two critical
benchmarks.
--
Joanna Carter [TeamB]
Consultant Software Engineer
.
- References:
- Re: Roll your own std::vector ???
- From: Peter Olcott
- Re: Roll your own std::vector ???
- From: Jon Skeet [C# MVP]
- Re: Roll your own std::vector ???
- From: Peter Olcott
- Re: Roll your own std::vector ???
- From: Jon Skeet [C# MVP]
- Re: Roll your own std::vector ???
- From: Peter Olcott
- Re: Roll your own std::vector ???
- From: Chris Mullins
- Re: Roll your own std::vector ???
- From: Peter Olcott
- Re: Roll your own std::vector ???
- From: Chris Mullins
- Re: Roll your own std::vector ???
- From: Peter Olcott
- Re: Roll your own std::vector ???
- From: Mark Wilden
- Re: Roll your own std::vector ???
- From: Peter Olcott
- Re: Roll your own std::vector ???
- From: Joanna Carter [TeamB]
- Re: Roll your own std::vector ???
- From: Chris Mullins
- Re: Roll your own std::vector ???
- From: Peter Olcott
- Re: Roll your own std::vector ???
- From: Joanna Carter [TeamB]
- Re: Roll your own std::vector ???
- From: Peter Olcott
- Re: Roll your own std::vector ???
- From: Jon Skeet [C# MVP]
- Re: Roll your own std::vector ???
- From: Peter Olcott
- Re: Roll your own std::vector ???
- From: Joanna Carter [TeamB]
- Re: Roll your own std::vector ???
- From: Peter Olcott
- Re: Roll your own std::vector ???
- From: Jon Skeet [C# MVP]
- Re: Roll your own std::vector ???
- From: Peter Olcott
- Re: Roll your own std::vector ???
- From: Joanna Carter [TeamB]
- Re: Roll your own std::vector ???
- Prev by Date: Re: Memory Limit for Visual Studio 2005???
- Next by Date: I can not access a share folder
- Previous by thread: Re: Roll your own std::vector ???
- Next by thread: Re: Roll your own std::vector ???
- Index(es):
Relevant Pages
|