-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.php
64 lines (50 loc) · 1.99 KB
/
demo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
$loadffi = microtime(true);
$goseg = FFI::load("libgoseg_tiny.h");
$endloadffi = microtime(true);
printf("load: " . ($endloadffi - $loadffi) . "\n");
require_once 'cast.php';
$files = makeGoStrSlice($goseg, ['dict/zh/dict.txt']);
$goseg->LoadDict($files);
$text = makeGoStr($goseg, '大数据存储与管理软件自适应技术计算摄像人工智能高并发高可用');
$result = $goseg->CutChar($text);
$words = FFI::string($result);
FFI::free($result);
printf("CutChar:%s\n", $words);
$result = makeGoStrSlice($goseg, []);
$goseg->CutSlice($text, FFI::addr($result));
$words = fromGoSlice($goseg, $result, 'GoString', 'fromGoStr');
printf("CutSlice:%s\n", implode(" ", $words));
FFI::free($result);
$len = makeGoInt($goseg, 0);
$cap = makeGoInt($goseg, 0);
$pointer = $goseg->CutPointer($text, FFI::addr($len), FFI::addr($cap));
$pSlice = $goseg->cast('void*', $pointer);
$words = fromGoSlicePointer($goseg, $pSlice, $len->cdata, 'GoString', 'fromGoStr');
printf("CutPointer:%s\n", implode(" ", $words));
$cutSum = 2000;
$startCut = microtime(true);
for ($i = 0; $i < $cutSum; $i++) {
$result = $goseg->CutChar($text);
$words = FFI::string($result);
}
$endCut = microtime(true);
printf("CutChar $cutSum 次用时:%f s\n", $endCut - $startCut);
$startCut = microtime(true);
for ($i = 0; $i < $cutSum; $i++) {
$result = makeGoStrSlice($goseg, []);
$goseg->CutSlice($text, FFI::addr($result));
$words = fromGoSlice($goseg, $result, 'GoString', 'fromGoStr');
}
$endCut = microtime(true);
printf("CutSlice $cutSum 次用时:%f s\n", $endCut - $startCut);
$startCut = microtime(true);
for ($i = 0; $i < $cutSum; $i++) {
$len = makeGoInt($goseg, 0);
$cap = makeGoInt($goseg, 0);
$pointer = $goseg->CutPointer($text, FFI::addr($len), FFI::addr($cap));
$pSlice = $goseg->cast('void*', $pointer);
$words = fromGoSlicePointer($goseg, $pSlice, $len->cdata, 'GoString', 'fromGoStr');
}
$endCut = microtime(true);
printf("CutPointer $cutSum 次用时:%f s\n", $endCut - $startCut);