- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有一组不重叠、不相邻的间隔,例如。 [{10,15}, {30,35}, {20,25}]。它们没有排序,但如果需要,我可以对它们进行排序。
现在我得到了一些新的时间间隔,例如。 {5,32} 并希望生成一组新的间隔来描述差异:这个新间隔所涵盖的范围不在集合中。在此示例中,答案为:[{5,9}, {16,19}, {26,29}]。
计算这个的快速算法是什么?请注意,该集合通常有 1 个,有时有 2 个,很少有 3 个或更多项目,所以我想针对这种情况进行优化。
对于上下文,这里是最初从开始+结束数据的输入流创建集合的代码,我会在其中合并:
type Interval struct {
start int
end int
}
func (i *Interval) OverlapsOrAdjacent(j Interval) bool {
return i.end+1 >= j.start && j.end+1 >= i.start
}
func (i *Interval) Merge(j Interval) bool {
if !i.OverlapsOrAdjacent(j) {
return false
}
if j.start < i.start {
i.start = j.start
}
if j.end > i.end {
i.end = j.end
}
return true
}
type Intervals []Interval
func (ivs Intervals) Len() int { return len(ivs) }
func (ivs Intervals) Swap(i, j int) { ivs[i], ivs[j] = ivs[j], ivs[i] }
func (ivs Intervals) Less(i, j int) bool { return ivs[i].start < ivs[j].start }
func (ivs Intervals) Merge(iv Interval) Intervals {
ivs = append(ivs, iv)
merged := make(Intervals, 0, len(ivs))
for _, iv := range ivs {
for i := 0; i < len(merged); {
if iv.Merge(merged[i]) {
merged = append(merged[:i], merged[i+1:]...)
} else {
i++
}
}
merged = append(merged, iv)
}
return merged
}
func (ivs Intervals) MergeUsingSort(iv Interval) Intervals {
ivs = append(ivs, iv)
sort.Sort(ivs)
merged := make(Intervals, 0, len(ivs))
merged = append(merged, ivs[0])
for i := 1; i < len(ivs); i++ {
last := len(merged) - 1
if !merged[last].Merge(ivs[i]) {
merged = append(merged, ivs[i])
}
}
return merged
}
func (ivs Intervals) Difference(iv Interval) Intervals {
// ???
return ivs
}
func main() {
var ivs Intervals
for _, input := range inputsFromSomewhere { // in reality, I don't have all these inputs at once, they come in one at a time
iv := Interval{input.start, input.end}
diffs := ivs.Difference(iv) // not yet implemented...
// do something with diffs
ivs = ivs.Merge(iv)
}
}
我发现上面的 Intervals.Merge() 比 MergeUsingSort() 快 2 倍,所以我想知道是否还有一种简单的非排序方式来回答我的问题。
最佳答案
问答代码不完整,无法编译。没有基准。快速浏览一下代码,它可能效率低下。
interval.go
和interval_test.go
的可用代码来自https://github.com/VertebrateResequencing/wr/tree/develop/minfys .
让我们从为区间差异示例编写基准开始。
package minfys
import (
"fmt"
"testing"
)
// Example
var (
xA = Intervals{{10, 15}, {30, 35}, {20, 25}}
xB = Interval{5, 32}
xD = Intervals{{5, 9}, {16, 19}, {26, 29}}
xR = Intervals{}
)
func BenchmarkExample(b *testing.B) {
b.ReportAllocs()
a := make(Intervals, len(xA))
b.ResetTimer()
for i := 0; i < b.N; i++ {
copy(a, xA)
xR = a.Difference(xB)
}
b.StopTimer()
if fmt.Sprint(xD) != fmt.Sprint(xR) {
b.Fatal(xD, xR)
}
}
接下来,编写一个 Difference 方法。
package minfys
func (a Intervals) Difference(b Interval) Intervals {
// If A and B are sets, then the relative complement of A in B
// is the set of elements in B but not in A.
// The relative complement of A in B is denoted B ∖ A:
// B \ A = {x ∈ A | x ∉ B}
// B \ A = B ∩ A'
//
// For example. d = a\b,
// a: [{10, 15}, {30, 35}, {20, 25}]
// b: {5,32}
// d: [{5,9}, {16,19}, {26,29}]
// The elements of set a are non-overlapping, non-adjacent,
// and unsorted intervals.
if len(a) <= 0 {
return Intervals{b}
}
d := make(Intervals, 0, 3)
for ; len(a) > 0; a = a[1:] {
for i := 1; i < len(a); i++ {
if a[i].Start < a[0].Start {
a[i], a[0] = a[0], a[i]
}
}
if b.Start < a[0].Start {
if b.End < a[0].Start {
d = append(d, b)
break
}
d = append(d, Interval{b.Start, a[0].Start - 1})
b.Start = a[0].Start
}
if b.End <= a[0].End {
break
}
if b.Start <= a[0].End {
b.Start = a[0].End + 1
}
if len(a) == 1 {
if b.Start <= a[0].End {
b.Start = a[0].End + 1
}
d = append(d, b)
break
}
}
return d
}
现在,对 Difference 方法进行基准测试。
BenchmarkExample-4 20000000 62.4 ns/op 48 B/op 1 allocs/op
sbs 写了一个 Difference 方法。
// Interval struct is used to describe something with a start and end. End must
// be greater than start.
type Interval struct {
Start int64
End int64
}
// Overlaps returns true if this interval overlaps with the supplied one.
func (i *Interval) Overlaps(j Interval) bool {
// https://nedbatchelder.com/blog/201310/range_overlap_in_two_compares.html
return i.End >= j.Start && j.End >= i.Start
}
// Intervals type is a slice of Interval.
type Intervals []Interval
// Difference returns any portions of iv that do not overlap with any of our
// intervals. Assumes that all of our intervals have been Merge()d in.
func (ivs Intervals) Difference(iv Interval) (diffs Intervals) {
diffs = append(diffs, iv)
for _, prior := range ivs {
for i := 0; i < len(diffs); {
if left, right, overlapped := prior.Difference(diffs[i]); overlapped {
if len(diffs) == 1 {
diffs = nil
} else {
diffs = append(diffs[:i], diffs[i+1:]...)
}
if left != nil {
diffs = append(diffs, *left)
}
if right != nil {
diffs = append(diffs, *right)
}
} else {
i++
}
}
if len(diffs) == 0 {
break
}
}
return
}
Benchmark sbs的Difference方法。
BenchmarkExample-4 5000000 365 ns/op 128 B/op 4 allocs/op
peterSO 的 Difference 方法明显更快。
old.txt (sbs) versus new.txt (peterSO):
benchmark old ns/op new ns/op delta
BenchmarkExample-4 365 62.4 -82.90%
benchmark old allocs new allocs delta
BenchmarkExample-4 4 1 -75.00%
benchmark old bytes new bytes delta
BenchmarkExample-4 128 48 -62.50%
这只是一个开始。可能还可以进行其他改进。
interval_test.go
中有一些错误。 ShouldBeNil
用于指针; ShouldBeEmpty
用于集合。 ShouldResemble
不处理集合相等性(包含相同元素的两个集合是同一个集合)。更改 ShouldResemble
顺序以匹配依赖于实现的顺序。
$ go test
..........................................................................................................................x......................................................x................x
Failures:
* interval_test.go
Line 247:
Expected: nil
Actual: '[]'
* interval_test.go
Line 375:
Expected: 'minfys.Intervals{minfys.Interval{Start:5, End:6}, minfys.Interval{Start:31, End:32}, minfys.Interval{Start:11, End:14}, minfys.Interval{Start:19, End:19}}'
Actual: 'minfys.Intervals{minfys.Interval{Start:5, End:6}, minfys.Interval{Start:11, End:14}, minfys.Interval{Start:19, End:19}, minfys.Interval{Start:31, End:32}}'
(Should resemble)!
* interval_test.go
Line 413:
Expected: 'minfys.Intervals{minfys.Interval{Start:7, End:10}, minfys.Interval{Start:1, End:3}, minfys.Interval{Start:15, End:17}}'
Actual: 'minfys.Intervals{minfys.Interval{Start:1, End:3}, minfys.Interval{Start:7, End:10}, minfys.Interval{Start:15, End:17}}'
(Should resemble)!
195 total assertions
...
198 total assertions
--- FAIL: TestIntervals (0.04s)
FAIL
.
$ diff -a -u ../interval_test.go interval_test.go
--- ../interval_test.go 2017-04-29 20:23:29.365344008 -0400
+++ interval_test.go 2017-04-29 20:54:14.349344903 -0400
@@ -244,19 +244,19 @@
So(len(ivs), ShouldEqual, 1)
newIvs = ivs.Difference(twoSix)
- So(newIvs, ShouldBeNil)
+ So(newIvs, ShouldBeEmpty)
ivs = ivs.Merge(twoSix)
So(len(ivs), ShouldEqual, 1)
newIvs = ivs.Difference(oneThree)
- So(newIvs, ShouldBeNil)
+ So(newIvs, ShouldBeEmpty)
ivs = ivs.Merge(oneThree)
So(len(ivs), ShouldEqual, 1)
oneSeven := Interval{1, 7}
newIvs = ivs.Difference(oneSix)
- So(newIvs, ShouldBeNil)
+ So(newIvs, ShouldBeEmpty)
ivs = ivs.Merge(oneSix)
So(len(ivs), ShouldEqual, 1)
@@ -372,7 +372,7 @@
fiveThirtyTwo := Interval{5, 32}
newIvs = ivs.Difference(fiveThirtyTwo)
- So(newIvs, ShouldResemble, Intervals{Interval{5, 6}, Interval{31, 32}, Interval{11, 14}, Interval{19, 19}})
+ So(newIvs, ShouldResemble, Intervals{Interval{5, 6}, Interval{11, 14}, Interval{19, 19}, Interval{31, 32}})
ivs = ivs.Merge(fiveThirtyTwo)
So(len(ivs), ShouldEqual, 3)
@@ -409,7 +409,7 @@
ivs = ivs.Truncate(17)
- expected := Intervals{sevenTen, oneThree, Interval{15, 17}}
+ expected := Intervals{oneThree, sevenTen, Interval{15, 17}}
So(ivs, ShouldResemble, expected)
})
})
.
$ go test
.............................................................................................................................................................................................................
205 total assertions
...
208 total assertions
PASS
$
I [@sbs] confirm it's faster than my solution. Though if you just measure the wall-time that using Difference() takes (put a before := time.Now() before the last Difference() call in the interval_test.go, and a time.Since(before) after it and sum those durations over the loop), it seems to make surprisingly little difference (on my machine it takes ~31ms with my solution and ~29ms with yours).
根据要求,修改了 interval_test.go
以测量挂钟时间:
$ diff -a -u ../interval_test.go walltime_test.go
--- ../interval_test.go 2017-04-29 20:23:29.365344008 -0400
+++ walltime_test.go 2017-04-30 13:39:29.000000000 -0400
@@ -24,6 +24,7 @@
"math/rand"
"testing"
"time"
+ "fmt"
)
func TestIntervals(t *testing.T) {
@@ -459,16 +460,20 @@
var ivs Intervals
errors := 0
+ var diffTime time.Duration
t := time.Now()
for i, input := range inputs {
iv := NewInterval(int64(input), int64(readSize))
+ before := time.Now()
newIvs := ivs.Difference(iv)
+ diffTime += time.Since(before)
if (len(newIvs) == 1) != exepectedNew[i] {
errors++
}
ivs = ivs.Merge(iv)
}
- // fmt.Printf("\ntook %s\n", time.Since(t))
+ fmt.Printf("took %s\n", time.Since(t))
+ fmt.Printf("\n Difference took %s\n", diffTime)
So(errors, ShouldEqual, 0)
So(len(ivs), ShouldEqual, 1)
So(time.Since(t).Seconds(), ShouldBeLessThan, 1) // 42ms on my machine
$
interval_test.go
基准输入大小和频率是
size frequency
0 1
1 94929
2 50072
3 4998
输出大小和频率是
size frequency
0 50000
1 100000
为此分布调整 peterSo 的差值方法给出
package minfys
func (a Intervals) Difference(b Interval) Intervals {
// If A and B are sets, then the relative complement of A in B
// is the set of elements in B but not in A.
// The relative complement of A in B is denoted B ∖ A:
// B \ A = {x ∈ A | x ∉ B}
// B \ A = B ∩ A'
//
// For example. d = a\b,
// a: [{10, 15}, {30, 35}, {20, 25}]
// b: {5,32}
// d: [{5,9}, {16,19}, {26,29}]
// The elements of set a are non-overlapping, non-adjacent,
// and unsorted intervals.
if len(a) <= 0 {
return Intervals{b}
}
var d Intervals
for ; len(a) > 0; a = a[1:] {
for i := 1; i < len(a); i++ {
if a[i].Start < a[0].Start {
a[i], a[0] = a[0], a[i]
}
}
if b.Start < a[0].Start {
if b.End < a[0].Start {
d = append(d, b)
break
}
d = append(d, Interval{b.Start, a[0].Start - 1})
b.Start = a[0].Start
}
if b.End <= a[0].End {
break
}
if b.Start <= a[0].End {
b.Start = a[0].End + 1
}
if len(a) == 1 {
if b.Start <= a[0].End {
b.Start = a[0].End + 1
}
d = append(d, b)
break
}
}
return d
}
为 peterSO 和 sbs 的 Difference 方法运行 interval_test.go
基准测试给出
$ go test -v
Merging many intervals is fast took 26.208614ms
Difference took 10.706858ms
和
$ go test -v
Merging many intervals is fast took 30.799216ms
Difference took 14.414488ms
peterSo 的差分法明显快于 sbs 的:10.706858 毫秒对 14.414488 毫秒或负 25.7%。
更新 peterSO 修改后的差异方法的早期示例基准测试结果
old.txt (sbs) versus new.txt (peterSO):
benchmark old ns/op new ns/op delta
BenchmarkExample-4 365 221 -39.45%
benchmark old allocs new allocs delta
BenchmarkExample-4 4 3 -25.00%
benchmark old bytes new bytes delta
BenchmarkExample-4 128 112 -12.50%
关于algorithm - 间隔和一组间隔之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43654953/
平时很少在jquery中用到this。查看代码时发现用到了,就调试出this的值,心想原来如此。还是挺有用的。这里总结一下this与$(this)的区别和使用。 $(this)生成的是什么?
使用单例类和应用程序范围的托管 bean 来保存应用程序数据有区别吗? 我需要查找某些 JNDI 资源,例如远程 bean 接口(interface),因此我为自己编写了一个单例来缓存我的引用并且只允
如果您仔细查看包含的图片,您会注意到您可以使用 Eclipse IDE 重构 Groovy 代码并将方法转换为闭包,反之亦然。那么,闭包到底是什么,它与方法有什么不同呢?有人可以举一个使用闭包的好例子
vagrant box repackage有什么区别( docs ) 和 vagrant package ( docs )? 我意识到 vagrant package仅适用于 VirtualBox 提
我想看看是否有人可以解释为什么以下代码适用于 valueOf 但不适用于其他代码。 import java.math.BigDecimal; public class Change { publ
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicates: What is Closures/Lambda in PHP or Javascript in layman te
This question already has answers here: Vagrant, Docker, Puppet, Chef (3个答案) 2年前关闭。 docker和chef有什么共同
以下代码在95%的机器上产生相同的输出,但是在几台机器上却有所不同。在 Debug模式下,输出: Changing from New to Fin OK 但在 Release模式下: Changing
////Creating Object var Obj; // init Object Obj= {}; 它们之间有什么区别两个? 有没有可能把它变成一个单行? 这样使用有什么好处吗?
我想找出定时器服务之间的区别。我应该使用哪个以及何时使用。我正在使用 Jboss 应用服务器。 1) java.ejb.Schedule。 @Schedule注解或配置自xml。 2) javax.e
我发现在 C++ 中可以通过三种不同的方式将对象传递给函数。假设我的类(class)是这样的: class Test { int i; public: Test(int x);
有什么区别。 public class Test { public static void main(String args[]) { String toBeCast = "c
如果我有一列,设置为主索引,设置为INT。 如果我不将其设置为自动递增,而只是将唯一的随机整数插入其中,与自动递增相比,这是否会减慢 future 的查询速度? 如果我在主索引和唯一索引为 INT 的
这两种日期格式有什么区别。第一个给出实际时间,第二个给出时间购买添加时区偏移值。 NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]
如果有一个函数,请说foo: function foo() { console.log('bar'); } 那么在 JavaScript 中,从另一个函数调用一个函数有什么区别,如下所示: f
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 4 年前。 Improv
代码是什么: class Time { private: int hours; int minutes; int seconds; pu
我知道这是非常基本的,但有人介意解释一下这两个数组声明之间的区别吗: #include array myints; ...和: int myints[5]; ...以及为什么 myints.size
我学会了如何根据 http://reference.sitepoint.com/css/specificity 计算 css 特异性但是,基于this reference,我不明白伪类(来自c)和伪元
为什么在运行 2) 时会出现额外的空行?对我来说 1 就像 2。那么为什么 2) 中的额外行? 1) export p1=$(cd $(dirname $0) && pwd) #
我是一名优秀的程序员,十分优秀!