@@ -81,6 +81,22 @@ func diffRebuildtexts(diffs []Diff) []string {
81
81
return text
82
82
}
83
83
84
+ func readFile (b * testing.B , filename string ) string {
85
+ bytes , err := ioutil .ReadFile (filename )
86
+ if err != nil {
87
+ b .Fatal (err )
88
+ }
89
+
90
+ return string (bytes )
91
+ }
92
+
93
+ func speedtestTexts (b * testing.B ) (s1 string , s2 string ) {
94
+ s1 = readFile (b , "../testdata/speedtest1.txt" )
95
+ s2 = readFile (b , "../testdata/speedtest2.txt" )
96
+
97
+ return s1 , s2
98
+ }
99
+
84
100
func Test_diffCommonPrefix (t * testing.T ) {
85
101
dmp := New ()
86
102
// Detect any common suffix.
@@ -1498,73 +1514,89 @@ func TestLastIndexOf(t *testing.T) {
1498
1514
}
1499
1515
}
1500
1516
1501
- func Benchmark_DiffMain (bench * testing.B ) {
1502
- dmp := New ()
1503
- dmp .DiffTimeout = time .Second
1504
- a := "`Twas brillig, and the slithy toves\n Did gyre and gimble in the wabe:\n All mimsy were the borogoves,\n And the mome raths outgrabe.\n "
1505
- b := "I am the very model of a modern major general,\n I've information vegetable, animal, and mineral,\n I know the kings of England, and I quote the fights historical,\n From Marathon to Waterloo, in order categorical.\n "
1517
+ func BenchmarkDiffMain (bench * testing.B ) {
1518
+ s1 := "`Twas brillig, and the slithy toves\n Did gyre and gimble in the wabe:\n All mimsy were the borogoves,\n And the mome raths outgrabe.\n "
1519
+ s2 := "I am the very model of a modern major general,\n I've information vegetable, animal, and mineral,\n I know the kings of England, and I quote the fights historical,\n From Marathon to Waterloo, in order categorical.\n "
1520
+
1506
1521
// Increase the text lengths by 1024 times to ensure a timeout.
1507
1522
for x := 0 ; x < 10 ; x ++ {
1508
- a = a + a
1509
- b = b + b
1523
+ s1 = s1 + s1
1524
+ s2 = s2 + s2
1510
1525
}
1526
+
1527
+ dmp := New ()
1528
+ dmp .DiffTimeout = time .Second
1529
+
1511
1530
bench .ResetTimer ()
1531
+
1512
1532
for i := 0 ; i < bench .N ; i ++ {
1513
- dmp .DiffMain (a , b , true )
1533
+ dmp .DiffMain (s1 , s2 , true )
1514
1534
}
1515
1535
}
1516
1536
1517
- func Benchmark_DiffCommonPrefix (b * testing.B ) {
1537
+ func BenchmarkDiffCommonPrefix (b * testing.B ) {
1538
+ s := "ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ"
1539
+
1518
1540
dmp := New ()
1519
- a := "ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ"
1541
+
1520
1542
for i := 0 ; i < b .N ; i ++ {
1521
- dmp .DiffCommonPrefix (a , a )
1543
+ dmp .DiffCommonPrefix (s , s )
1522
1544
}
1523
1545
}
1524
1546
1525
- func Benchmark_DiffCommonSuffix (b * testing.B ) {
1547
+ func BenchmarkDiffCommonSuffix (b * testing.B ) {
1548
+ s := "ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ"
1549
+
1526
1550
dmp := New ()
1527
- a := "ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ"
1551
+
1552
+ b .ResetTimer ()
1553
+
1528
1554
for i := 0 ; i < b .N ; i ++ {
1529
- dmp .DiffCommonSuffix (a , a )
1555
+ dmp .DiffCommonSuffix (s , s )
1530
1556
}
1531
1557
}
1532
1558
1533
- func Benchmark_DiffMainLarge (b * testing.B ) {
1534
- s1 := readFile ( "../testdata/speedtest1.txt" , b )
1535
- s2 := readFile ( "../testdata/speedtest2.txt" , b )
1559
+ func BenchmarkDiffMainLarge (b * testing.B ) {
1560
+ s1 , s2 := speedtestTexts ( b )
1561
+
1536
1562
dmp := New ()
1563
+
1537
1564
b .ResetTimer ()
1565
+
1538
1566
for i := 0 ; i < b .N ; i ++ {
1539
1567
dmp .DiffMain (s1 , s2 , true )
1540
1568
}
1541
1569
}
1542
1570
1543
- func Benchmark_DiffMainLargeLines (b * testing.B ) {
1544
- s1 := readFile ( "../testdata/speedtest1.txt" , b )
1545
- s2 := readFile ( "../testdata/speedtest2.txt" , b )
1571
+ func BenchmarkDiffMainRunesLargeLines (b * testing.B ) {
1572
+ s1 , s2 := speedtestTexts ( b )
1573
+
1546
1574
dmp := New ()
1575
+
1547
1576
b .ResetTimer ()
1577
+
1548
1578
for i := 0 ; i < b .N ; i ++ {
1549
1579
text1 , text2 , linearray := dmp .DiffLinesToRunes (s1 , s2 )
1580
+
1550
1581
diffs := dmp .DiffMainRunes (text1 , text2 , false )
1551
1582
diffs = dmp .DiffCharsToLines (diffs , linearray )
1552
1583
}
1553
1584
}
1554
1585
1555
- func Benchmark_DiffHalfMatch (b * testing.B ) {
1556
- s1 := readFile ( "../testdata/speedtest1.txt" , b )
1557
- s2 := readFile ( "../testdata/speedtest2.txt" , b )
1586
+ func BenchmarkDiffHalfMatch (b * testing.B ) {
1587
+ s1 , s2 := speedtestTexts ( b )
1588
+
1558
1589
dmp := New ()
1590
+
1559
1591
b .ResetTimer ()
1592
+
1560
1593
for i := 0 ; i < b .N ; i ++ {
1561
1594
dmp .DiffHalfMatch (s1 , s2 )
1562
1595
}
1563
1596
}
1564
1597
1565
- func Benchmark_DiffCleanupSemantic (b * testing.B ) {
1566
- s1 := readFile ("../testdata/speedtest1.txt" , b )
1567
- s2 := readFile ("../testdata/speedtest2.txt" , b )
1598
+ func BenchmarkDiffCleanupSemantic (b * testing.B ) {
1599
+ s1 , s2 := speedtestTexts (b )
1568
1600
1569
1601
dmp := New ()
1570
1602
@@ -1576,11 +1608,3 @@ func Benchmark_DiffCleanupSemantic(b *testing.B) {
1576
1608
dmp .DiffCleanupSemantic (diffs )
1577
1609
}
1578
1610
}
1579
-
1580
- func readFile (filename string , b * testing.B ) string {
1581
- bytes , err := ioutil .ReadFile (filename )
1582
- if err != nil {
1583
- b .Fatal (err )
1584
- }
1585
- return string (bytes )
1586
- }
0 commit comments