小编Ste*_*las的帖子

最有效的方法来检查$ string是否以perl中的$ needle开头

鉴于两个字符串变量$string$needlein perl,什么是检查是否$string开始的最有效方法$needle.

  • $string =~ /^\Q$needle\E/ 是我能想到的最接近的匹配,它做了所需要的但是效率最低(到目前为止)我尝试过的解决方案.
  • index($string, $needle) == 0对某些值有效并且相对有效$string,$needle但不必要地在其他位置搜索针(如果在开始时没有找到).
  • substr($string, 0, length($needle)) eq $needle 应该是非常简单和有效的,但在我的几个测试中,大多数测试并不比前一个测试更有效.

是否有规范的方法可以做到这一点perl,我不知道或以任何方式优化任何上述解决方案?

(在我的特定用例中,$string并且$needle在每次运行中将会有所不同,因此预编译正则表达式不是一种选择).


如何衡量给定解决方案性能的示例(此处来自POSIX sh):

string='somewhat not so longish string' needle='somew'
time perl -e '
  ($n,$string,$needle) = @ARGV;
  for ($i=0;$i<$n;$i++) {

    index($string, $needle) == 0

  }' 10000000 "$string" "$needle"
Run Code Online (Sandbox Code Playgroud)

使用这些值,使用perl 5.14.2 index()substr()+eq使用此系统更好,但是:

string="aaaaabaaaaabaaaaabaaaaabaaaaabaaaaab" needle="aaaaaa"
Run Code Online (Sandbox Code Playgroud)

那是相反的.

perl performance string-matching

24
推荐指数
2
解决办法
3万
查看次数

获取systemtap中目标数组的大小

姐妹网站答案中,我试图从Linux内核数组中转储信息,该数组unix_socket_table@net/unix/af_unix.c定义为:

struct hlist_head unix_socket_table[2 * UNIX_HASH_SIZE];
Run Code Online (Sandbox Code Playgroud)

目前,我在stp脚本中硬编码数组的大小:

for (i = 0; i < 512; i++)

我怎么能避免这种情况?该信息(数组的大小)存储在调试信息中.gdb可以告诉我:

$ gdb --batch --ex 'whatis unix_socket_table' "/usr/lib/debug/boot/vmlinux-$(uname -r)"
type = struct hlist_head [512]
$ gdb --batch --ex 'p sizeof(unix_socket_table)/sizeof(*unix_socket_table)' "/usr/lib/debug/boot/vmlinux-$(uname -r)"
$1 = 512
Run Code Online (Sandbox Code Playgroud)

但是我该怎么办呢systemtap?AFAICT,systemtap没有sizeof()运营商.

linux arrays systemtap

2
推荐指数
1
解决办法
453
查看次数

标签 统计

arrays ×1

linux ×1

performance ×1

perl ×1

string-matching ×1

systemtap ×1