Connecting Tech Pros Worldwide Forums | Help | Site Map

How to convert from dynamic polymophism to static polymophism?

PengYu.UT@gmail.com
Guest
 
Posts: n/a
#1: Nov 9 '05
Hi,

Some dynamic polymorphism programs can be converted to the equavalent
static polymorphism programs. I'm wondering if there are any generall
procedures that I can use to do this conversion.

Best wishes,
Peng


Puppet_Sock
Guest
 
Posts: n/a
#2: Nov 9 '05

re: How to convert from dynamic polymophism to static polymophism?


PengYu.UT@gmail.com wrote:[color=blue]
> Some dynamic polymorphism programs can be converted to the equavalent
> static polymorphism programs. I'm wondering if there are any generall
> procedures that I can use to do this conversion.[/color]

Sort of makes the question "Why would you want to do that?" pop
into my mind.

Also sort of makes the question "What do you mean by static
polymorphism?"
pop into my mind. Do you mean compile time as opposed to run time?

I'm thinking that, once you nail down a little more carefully what you
mean,
you will find you either don't want to do that, or there is no general
way of
doing that only case-specific ways.
Socks

PengYu.UT@gmail.com
Guest
 
Posts: n/a
#3: Nov 9 '05

re: How to convert from dynamic polymophism to static polymophism?



Puppet_Sock wrote:[color=blue]
> PengYu.UT@gmail.com wrote:[color=green]
> > Some dynamic polymorphism programs can be converted to the equavalent
> > static polymorphism programs. I'm wondering if there are any generall
> > procedures that I can use to do this conversion.[/color]
>
> Sort of makes the question "Why would you want to do that?" pop
> into my mind.[/color]

Most design pattern examples are shown by declaring an abstract class
and using inheritance. But this kind of inheritance hierachy requires
virtual functions which have some overhead in terms of runtime. If
those pattern examples can be converted into using static polymorphism,
the virtual functions can be eliminated and the runtime should be
better.
[color=blue]
>
> Also sort of makes the question "What do you mean by static
> polymorphism?"
> pop into my mind. Do you mean compile time as opposed to run time?[/color]

What I mean is using type information to figure out what class(with
template) to use at compile time (used in STL).
[color=blue]
>
> I'm thinking that, once you nail down a little more carefully what you
> mean,
> you will find you either don't want to do that, or there is no general
> way of
> doing that only case-specific ways.
> Socks[/color]

Puppet_Sock
Guest
 
Posts: n/a
#4: Nov 9 '05

re: How to convert from dynamic polymophism to static polymophism?


PengYu.UT@gmail.com wrote:[color=blue]
> Puppet_Sock wrote:[color=green]
> > PengYu.UT@gmail.com wrote:[color=darkred]
> > > Some dynamic polymorphism programs can be converted to the equavalent
> > > static polymorphism programs. I'm wondering if there are any generall
> > > procedures that I can use to do this conversion.[/color]
> >
> > Sort of makes the question "Why would you want to do that?" pop
> > into my mind.[/color]
>
> Most design pattern examples are shown by declaring an abstract class
> and using inheritance. But this kind of inheritance hierachy requires
> virtual functions which have some overhead in terms of runtime. If
> those pattern examples can be converted into using static polymorphism,
> the virtual functions can be eliminated and the runtime should be
> better.[/color]

Maybe. Or not. If you are worried about the amount of overhead involved
in virtual functions, you are likely barking up the wrong tree to start
with.
Particularly if you have not measured the difference in specific cases
that are relevant to your application. In the bulk of cases, whatever
difference
there may be is trivial. And it is not necessarily the case that the
template
is faster.

But suppose the template is faster. And suppose that it makes your
code run, overall, faster by a couple milli-seconds, out of a total run
time of many hours. That's what I mean by "relevant to your
application."
If you have not measured the difference then it's silly to try to
figure out
any generic way of making the transformation. It's not generally
important,
and it's not really possible to do generically anyway.
[color=blue][color=green]
> > Also sort of makes the question "What do you mean by static
> > polymorphism?"
> > pop into my mind. Do you mean compile time as opposed to run time?[/color]
>
> What I mean is using type information to figure out what class(with
> template) to use at compile time (used in STL).[/color]

Further, part of the point of run-time polymorphism is that you don't
want to have to figure out the type to put in the slot at compile time.
Consider, for example, the idea of using a factory. You want to be
able to substitute a child for a parent based on things that are not
known until run-time. Just as a trivial example, you might want to
base a choice on user input. That's a big part of why such things as
the factory pattern were invented.
Socks

mlimber
Guest
 
Posts: n/a
#5: Nov 9 '05

re: How to convert from dynamic polymophism to static polymophism?


Puppet_Sock wrote:[color=blue]
> PengYu.UT@gmail.com wrote:[color=green]
> > Puppet_Sock wrote:[color=darkred]
> > > PengYu.UT@gmail.com wrote:
> > > > Some dynamic polymorphism programs can be converted to the equavalent
> > > > static polymorphism programs. I'm wondering if there are any generall
> > > > procedures that I can use to do this conversion.
> > >
> > > Sort of makes the question "Why would you want to do that?" pop
> > > into my mind.[/color]
> >
> > Most design pattern examples are shown by declaring an abstract class
> > and using inheritance. But this kind of inheritance hierachy requires
> > virtual functions which have some overhead in terms of runtime. If
> > those pattern examples can be converted into using static polymorphism,
> > the virtual functions can be eliminated and the runtime should be
> > better.[/color]
>
> Maybe. Or not. If you are worried about the amount of overhead involved
> in virtual functions, you are likely barking up the wrong tree to start
> with.
> Particularly if you have not measured the difference in specific cases
> that are relevant to your application. In the bulk of cases, whatever
> difference
> there may be is trivial. And it is not necessarily the case that the
> template
> is faster.
>
> But suppose the template is faster. And suppose that it makes your
> code run, overall, faster by a couple milli-seconds, out of a total run
> time of many hours. That's what I mean by "relevant to your
> application."
> If you have not measured the difference then it's silly to try to
> figure out
> any generic way of making the transformation. It's not generally
> important,
> and it's not really possible to do generically anyway.[/color]

I agree with Puppet Sock about premature optimization. If, however, you
have shown with a profiler that the virtual function call is costing
you too much, you might switch over to static polymorphism to gain some
speed (probably trading some ease of use and code bloat). The FAQ gives
an example of this
(http://www.parashift.com/c++-faq-lit...tml#faq-33.11).
I will reiterate: do not PRESUME that the virtual function is too
expensive.

As for implementing it generally, check out the "Template Method"
pattern in _Design Patterns_. They don't define the conversion
procedure you're looking for, but that chapter could give you a start.

Cheers! --M

Closed Thread