gpt4 book ai didi

Flash/Actionscript CPU 分析器

转载 作者:行者123 更新时间:2023-12-03 14:39:06 28 4
gpt4 key购买 nike

你找到这样的工具并成功使用了吗?

最佳答案

我也在寻找 AS 的分析器,但我想要一个与 FlashDevelop 和 Flex SDK 配合使用的免费软件/开源解决方案。我没有找到。所以我写了一个简单的python脚本和一个更简单的AS类。该脚本基本上采用任何 AS 文件并将分析代码(即以 1 毫秒的精度测量该函数的总运行时间的调用 - flash.utils.getTimer() 调用的分辨率)添加到每个函数定义。脚本有时会出错,但这些通常很容易手动修复。然后您需要手动再添加一行:在某个时刻转储分析统计信息。这种方法显然不够准确,但它仍然让您对代码中的瓶颈有很好的感觉。我成功地将它用于 100k 文件。

这是AS类:

package  {
public class Profiler {
private static var instance:Profiler;

public static function get profiler():Profiler {
if (!Profiler.instance) Profiler.instance = new Profiler;
return Profiler.instance;
}

private var data:Object = {};

public function profile(fn:String, dur:int):void {
if (!data.hasOwnProperty(fn)) data[fn] = new Number(0);
data[fn] += dur / 1000.0;
}

public function clear():void {
data = { };
}

public function get stats():String {
var st:String = "";
for (var fn:String in data) {
st += fn + ":\t" + data[fn] + "\n";
}
return st;
}
}
}

这是可以解决问题的python脚本:
import sre, sys

rePOI = sre.compile(r'''\bclass\b|\bfunction\b|\breturn\b|["'/{}]''')
reFun = sre.compile(r'\bfunction\b\s*((?:[gs]et\s+)?\w*)\s*\(')
reCls = sre.compile(r'class\s+(\w+)[\s{]')
reStr = sre.compile(r'''(["'/]).*?(?<!\\)\1''')

def addProfilingCalls(body):
stack = []
pos = 0
depth = 0
retvar = 0
klass = ""
match = rePOI.search(body, pos)
while match:
poi = match.group(0)
pos = match.start(0)
endpos = match.end(0)

if poi in '''"'/''':
strm = reStr.match(body, pos)
if strm and (poi != '/' or sre.search('[=(,]\s*$', body[:pos])):
endpos = strm.end(0)

elif poi == 'class':
klass = reCls.match(body, pos).group(1)
sys.stderr.write('class ' + klass + '\n')

elif poi == 'function':
fname = reFun.match(body, pos)
if fname.group(1):
fname = klass + '.' + fname.group(1)
else:
lastf = stack[-1]
lastf['anon'] += 1
fname = lastf['name'] + '.anon' + str(lastf['anon'])
sys.stderr.write('function ' + fname + '\n')
stack.append({'name':fname, 'depth':depth, 'anon':0})

brace = body.find('{', pos) + 1
line = "\nvar __start__:int = flash.utils.getTimer();"
body = body[:brace] + line + body[brace:]
depth += 1
endpos = brace + len(line)

elif poi == '{':
depth += 1

elif poi == 'return':
lastf = stack[-1]
semicolon = body.find(';', pos) + 1
if sre.match('return\s*;', body[pos:]):
line = "{ Profiler.profiler.profile('" + lastf['name'] + \
"', flash.utils.getTimer() - __start__); return; }"
else:
retvar += 1
line = "{ var __ret" + str(retvar) + "__:* =" + body[pos+6:semicolon] + \
"\nProfiler.profiler.profile('" + lastf['name'] + \
"', flash.utils.getTimer() - __start__); return __ret" + str(retvar) + "__; }"
body = body[:pos] + line + body[semicolon:]
endpos = pos + len(line)

elif poi == '}':
depth -= 1
if len(stack) > 0 and stack[-1]['depth'] == depth:
lastf = stack.pop()
line = "Profiler.profiler.profile('" + lastf['name'] + \
"', flash.utils.getTimer() - __start__);\n"
body = body[:pos] + line + body[pos:]
endpos += len(line)

pos = endpos
match = rePOI.search(body, pos)
return body

def main():
if len(sys.argv) >= 2: inf = open(sys.argv[1], 'rU')
else: inf = sys.stdin
if len(sys.argv) >= 3: outf = open(sys.argv[2], 'wU')
else: outf = sys.stdout
outf.write(addProfilingCalls(inf.read()))
inf.close()
outf.close()

if __name__ == "__main__":
main()

随意使用、分发和修改两者。

关于Flash/Actionscript CPU 分析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/357825/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com