在运行时更改内部表示 [英] Changing internal representation in runtime

查看:87
本文介绍了在运行时更改内部表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新 主要问题仍然是 在这个例子下,但我想它归结为 到:

UPDATE The main questions remain the ones under the example, but I guess it boils down to :

**如果您的类型可以用一个表示99%的值 快速,强大的类型,并且仅占1% 非常重的类型,(例如int vs. BigInteger)如何表示? **

**If you have a type where 99% of the values could be represented in one fast, powerfull type, and only 1% in a very heavy type, (say int vs. BigInteger) How to represent it?? **

一所学校,我们学到了很多有关内部表示的知识,但是从来没有在运行时更改它的方法.我的意思是:假设您有一个代表小数的类,但是您使用整数在内部表示它,直到实际上您需要一个比该整数更大的值,并且只比更改表示法...

A school we learned a lot about internal representations, but never how to change it at runtime. I mean : suppose you have a class representing a decimal, but you use an integer to represent it internal, until you actually need a bigger value than the integer, and only than change representation...

我以前从没想过,当想到它时,我以为那是行不通的,因为所有的检查都会杀死它.但是我只是做了一个测试,因为我对自己的利益太好奇了,并且确实存在表示形式更改更麻烦的情况:给定此接口:

I never thought of this before, and when thinkihng of it, I thought that would never work, since all the checks would kill it. But I just did a test since I'm too curious for my own good and there do exist situations when changing of representation is more perormant : given this interface :

interface INumber
    {
        void add1000();
        void SetValue(decimal d);
        decimal GetValue();                     
    } 

我发现这两种实施方式中的后一种在很多情况下都更强大,包括这一种我致力于吸引尽可能多的想法(不是代表,而是社区)

I found the latter of the two implementations to be more powerful in a lot of situations, including this one that I composed to attract as many ideas I could on the matter (not rep, it's community)

    1. Representation by only a decimal

        public class Number1:INumber
        {

            private decimal d { get; set; }


            public void add1000()
            {
                d += 1000;
            }



            public decimal GetValue()
            {
                return d;
            }



            public void SetValue(decimal d)
            {
                this.d = d;
            }

        }


2. Representation by a decimal and an int

public class Number2:INumber
    {
        private bool usedecimal; 
        private int i;
        private decimal d;

        public void add1000()
        {
            if (usedecimal)
            {
                d += 1000;
                return; 
            }

            i += 1000;

            if (i > 2147480000)
            {
                d = i;              
                usedecimal = true;              
            }


        }

        public void SetValue(decimal d)
        {
            try
            {
                i = (int)d;

            }
            catch (OverflowException e)
            {

                this.d = d;
            }

        }

        public decimal GetValue()
        {
            return Math.Max(i,d);
        }
    }
}

我的问题如下:

这似乎…….我一直很想念,但是这一定是明显的流血.谁能帮我这个忙吗?

This seems sth. I have been missing, but this must be the bleeding obvious. Can anyone help me out with this?

  • 是否有关于混合表示的准则,何时使用它们,何时不使用?
  • 在没有基准测试的情况下,混合表示可以更快时,如何预感呢?
  • 有什么例子吗?
  • 有什么图案吗?
  • 对此有何想法?

推荐答案

如果您有一个类型,其中99%的值可以用一种快速,强大的类型表示,而只有1%的类型可以用非常重的类型表示,(例如int vs. BigInteger)如何表示??

If you have a type where 99% of the values could be represented in one fast, powerfull type, and only 1% in a very heavy type, (say int vs. BigInteger) How to represent it??

BigInteger实现通常可以做到这一点;他们将所有内容保持为整数或多位,直到出现溢出为止,然后才进行重量级的实现.

BigInteger implementations typically do exactly that; they keep everything in ints or longs until something overflows, and only then do they go to the heavierweight implementation.

有很多方法可以表示它.我喜欢的模式是:

There's any number of ways to represent it. A pattern I like is:

public abstract class Thing
{
    private class LightThing : Thing
    { ... }
    private class HeavyThing : Thing 
    { ... }
    public static Thing MakeThing(whatever) 
    { /* make a heavy or light thing, depending */ }
    ... etc ...
}

是否有关于混合表示的准则,何时使用它们,何时不使用?

Are there guidelines for mixed representations, when to use them, when not?

好的.我们可以轻松地编译这样的列表.这种技术在以下情况下有意义:

Sure. We can easily compile such a list. This technique makes sense if:

(1)轻量级实现比重量级实现更轻

(1) the lightweight implementation is much lighter than the heavyweight implementation

(2)大多数情况下,典型用法都属于轻量级代码路径

(2) the typical usage falls into the lightweight code path most of the time

(3)与重量级解决方案的成本相比,检测过渡的成本不是一个重大成本.

(3) the cost of detecting the transition is not a significant cost compared to the cost of the heavyweight solution

(4)为了实现以客户为中心,切合实际的性能目标,必须使用更复杂的两种表示形式的解决方案.

(4) the more complex two-representation solution is necessary in order to achieve a customer-focused, realistic performance goal.

在没有基准测试的情况下,混合表示可以更快时,如何预感呢?

How to have a hunch when a mixed represtenation can be faster without benchmarking?

不要.根据预感做出绩效决策是先于事实的推理.根据 realistic customer focus data-driven 分析来驱动性能决策,而不是凭直觉.如果多年来我已经了解了有关性能分析的一件事,那就是我的直觉通常是错误的.

Don't. Making performance decisions based on hunches is reasoning in advance of facts. Drive performance decisions on realistic, customer focused, data-driven analysis, not on hunches. If I've learned one thing about performance analysis over the years its that my hunches are usually wrong.

有什么例子吗?

Any examples?

任何数量的BigInteger实现.

Any number of implementations of BigInteger.

有任何模式吗?

Any patterns?

击败我.我不是很多记住模式分类法的人.

Beats the heck out of me. I'm not much of one for memorizing pattern taxonomies.

有什么想法吗?

Any ideas on the matter?

请参阅上文.

这篇关于在运行时更改内部表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆