数值模式匹配(Numerical Pattern Matching)
我正在研究的项目需要一些数字模式匹配。 我的搜索没有发现许多相关的点击,因为大多数结果往往是围绕文本模式匹配。 我们的想法是,我们需要注意某些波形模式,并尝试将输入数据与我们将要构建的波形数据库进行匹配。 这是我们需要匹配的一种波形的示例。
alt text http://tmp.stayhealthy.com/wave.png
那里有一个明显的模式,但峰值不会有完全相同的值,但波次迭代的整体形状将非常相似。 有没有人对如何存储和以后匹配这些模式和/或其他搜索术语有任何建议,我可以用它来找到关于模式匹配主题的更多信息?
蒂姆,谢谢。
A project I'm researching requires some numerical pattern matching. My searches haven't turned up many relevant hits since most results tend to be around text pattern matching. The idea is we'll have certain wave patterns we'll need to be watching for and trying to match incoming data vs the wave database we will be building. Here is and example of one of the wave patterns we'll need to be matching against.
alt text http://tmp.stayhealthy.com/wave.png
There is clearly a pattern there, but the peaks will not have the exact same values, but the overall shape of the wave iterations will be very similar. Does anyone have any advice on how to go about storing and later matching these patterns, and / or other search terms I can use to find more information on the subject of pattern matching?
Thanks, Tim.
原文:https://stackoverflow.com/questions/2669470
最满意答案
如果您担心做某事的成本,那就去衡量一下。 如果你无法衡量差异,那么差异可能无关紧要(如果重要的话,你就可以衡量它)。
要回答更具体的问题,变量没有任何内在成本。 在CPU上实际执行的代码中不存在变量。 它们是源级构造,并且编译器没有义务在源代码变量之间创建某种一对一映射,例如,执行
push
指令,或使用的寄存器或其他任何东西。 代码中的单个变量可以对应于使用多个不同的寄存器(编译器可能会选择不时地移动它),或者几个不同的变量可能最终共享同一个寄存器,因为它们从未在同时。编译器所做的许多事情之一是确定变量的生命周期。 何时首次使用,何时使用? 当你声明它或它超出范围时,无关紧要。
编译器通常使用的转换将您的代码放入更适合优化的形式,即将每个赋值转换为一个新的,唯一的变量,基本上与第二个示例类似。 这就是编译器喜欢使用代码的方式,因为现在它可以清楚地看到依赖关系。 在第一种情况下,看起来整个函数中必须存在相同的变量,并且可以重用这两个操作。 在第二种情况下,我们可以很容易地看到它们是独立的,独立的变量,并且我们可以看到它们的生命周期不重叠(在声明
rateNode
之后不使用rateNode
)。两个版本都可能生成完全相同的代码,但第二个版本更接近于优化编译器内部对代码所做的操作。
If you're worried about the cost of doing something, then measure it. If you can't measure a difference, then the difference can not possibly matter (if it mattered, you'd be able to measure it).
To answer your more specific question, a variable does not have any intrinsic cost. Variables do not exist in the code that is actually executed on the CPU. They are a source-level construct, and the compiler is not obliged to create some kind of 1-to-1 mapping between source code variables and, say,
push
instructions executed, or registers used, or anything else. A single variable in your code can correspond to the use of several different registers (the compiler might choose to move it around from time to time), or several different variables might all end up sharing the same register because they're never used at the same time.One of the many things a compiler does is determine the lifetime of a variable. When is it first used, and when is it last used? It doesn't matter when you declared it, or when it goes out of scope.
A transformation commonly used by compilers to put your code into a form more amenable to optimizations is to turn every assignment into a new, unique, variable, basically like in your second example. That's how the compiler prefers to work with the code, because now it can see the dependencies clearly. In the first case, it looks as if the same variable has to exist throughout the function, and be reused for both operations. In the second case, we can easily see that they're separate, independent, variables, and we can see that their lifetimes do not overlap (
idNode
is not used after the declaration ofrateNode
).Both versions will likely produce the exact same code, but the second version is closer to what optimizing compilers do to the code internally.
相关问答
更多-
什么是“可变范围”? 变量具有有限的“范围”或“可访问的地方”。 只是因为你写了$foo = 'bar'; 一旦你的应用程序的某个地方并不意味着您可以从应用程序内的其他地方引用$foo 。 变量$foo具有一定的范围,在该范围内它是有效的,只有相同范围内的代码才能访问该变量。 PHP中定义的范围如何? 很简单:PHP具有函数范围 。 这是PHP中唯一存在的范围分隔符。 函数内的变量只能在该函数内部使用。 功能之外的变量可以在函数之外的任何地方使用,但不在任何函数内。 这意味着PHP中有一个特殊的范围: 全局 ...
-
8.3.2参考文献§4 未指定参考是否需要存储 也就是说,如果引用需要存储,它通常需要与指针一样多的存储空间: struct P { int* p; }; struct R { int& r; }; static_assert(sizeof(P) == sizeof(R), "sizeof(P) == sizeof(R)"); 8.3.2 References §4 It is unspecified whether or not a reference requires storag ...
-
更新参考变量(Update reference variables)[2022-12-09]
我认为你想要做的是传递对象引用 var obj = { var: 'initial' }; var a = [{ test: 'old', new: 'no' }, { test: obj, new: 'yes' }]; var o = { test: obj, new: 'yes' }; obj.var = 'objModified'; alert(o.test.var); // Changes correctly obj.var = 'arrModified'; alert(o.test.var ... -
我可以想象几种方法: 一些代码像 sysuse auto, clear quietly ds local myvar `:word 5 of `r(varlist)'' display "`myvar'"
会让你在不知道名字的情况下使用第五个变量。 您可以使用循环并以这种方式定义变量列表。 这基本上是http://www.stata.com/statalist/archive/2010-03/msg00073.html的回应。 将您的数 ... -
部分参考中的变量(Variable in partial reference)[2021-09-21]
这将被称为“动态部分” - 您可以在Handlebars文档中查看。 可以通过使用子表达式语法来动态选择要执行的部分。 {{> (whichPartial) }}将评估whichPartial,然后呈现此函数返回其名称的部分。 子表达式不能解析变量,因此哪些部分必须是函数。 如果一个简单变量具有部分名称,则可以通过查找帮助程序解析它。 {{> (lookup . 'myVariable') }} This would be called "dynamic partial" – you can check h ... -
参考或变量?(Reference or variable?)[2020-02-03]
如果使用&$foo通过引用显式传递变量,则只通过引用(在当前版本的PHP中)传递变量。 同样,在将变量声明为新变量(例如$foo = $bar ,$ foo将是对$ bar的引用,直到值发生变化。 然后它是一个新的副本。 这里有很多检测参考的方法,也许可以检查一些参考 。 (为什么你需要这样做是未知的,但它仍然存在)。 http://www.php.net/manual/en/language.references.spot.php A variable is only passed by referenc ... -
“引用”(变量别名)使您的代码更难理解,并且可能成为难以理解的错误的来源。 没有正当理由在php中使用引用并且在更安全的一方尝试完全避免它们。 不,php5中的对象与“引用”无关。 在PHP中实现的“引用”是一个奇怪的概念。 通常,在编程语言中,变量彼此独立,因此更改一个变量不会影响其他变量。 Php“引用”允许多个变量共享相同的值并且彼此依赖。 基本上,你改变了一个变量,而你认为完全不相关的另一个变量也会发生变化。 这不是好事,经常导致很多混乱。 php中的对象(我需要添加'five'?)与上述意义上的“ ...
-
如果您担心做某事的成本,那就去衡量一下。 如果你无法衡量差异,那么差异可能无关紧要(如果重要的话,你就可以衡量它)。 要回答更具体的问题,变量没有任何内在成本。 在CPU上实际执行的代码中不存在变量。 它们是源级构造,并且编译器没有义务在源代码变量之间创建某种一对一映射,例如,执行push指令,或使用的寄存器或其他任何东西。 代码中的单个变量可以对应于使用多个不同的寄存器(编译器可能会选择不时地移动它),或者几个不同的变量可能最终共享同一个寄存器,因为它们从未在同时。 编译器所做的许多事情之一是确定变量的生 ...
-
你必须复制你的表格。 You'll have to make a copy of your table.
-
在C#中使用引用参数而不是使用全局变量的原因(Reason for use reference parameters instead of using global variable in C#)[2022-02-08]
它只是糟糕的编程! 假设你已经完成了你的程序,然后一个月后你想回来添加其他功能。 但是你意外地在新类中的全局变量中使用了它。 现在它改变了你不想改变的其他类中的东西。 你有更多的错误,你不知道从哪里开始,因为你没有触摸代码一个月。 关于ref词还有一件事 - 它的目标之一是在返回变量之前强制初始化变量。 这样编译器确保你不使用包含垃圾的变量来破坏代码。 当您使用全局变量时,您可以在该未初始化的全局变量中使用,并且您将获得异常。 因此,如果你知道何时以及如何使用ref和out - 使用它们。 并且不要使用全局 ...