追踪一下调用
2025-07-31 17:09:59
发布于:浙江
3阅读
0回复
0点赞
加上一点点log,有点壮观
#include <cstdio>
#include <algorithm>
using namespace std;
long long arr[25][25][25];
long long a,b,c;
long long pid;
bool flag = true;
long long w(long long a,long long b,long long c,long long ppid){
long long mpid = pid++;
if(a <= 0 || b <= 0 || c <= 0){
if(flag)printf("%lld-->%lld=%lld %lld %lld:1\n",ppid,mpid,a,b,c);
return 1;
}
if(a > 20 || b >20 || c > 20){
long long res = w(20,20,20,mpid);
if(flag)printf("%lld-->%lld=%lld %lld %lld:%lld\n",ppid,mpid,a,b,c,res);
return res;
}
if(arr[a][b][c] != 0){
if(flag)printf("%lld-->%lldcache: %lld %lld %lld:%lld\n",ppid,mpid,a,b,c,arr[a][b][c]);
return arr[a][b][c];
}
if(a < b && b < c){
arr[a][b][c] = w(a,b,c-1,mpid)+w(a,b-1,c-1,mpid)-w(a,b-1,c,mpid);
if(flag)printf("%lld-->%lld=%lld %lld %lld:%lld\n",ppid,mpid,a,b,c,arr[a][b][c]);
return arr[a][b][c];
}
arr[a][b][c] = w(a-1,b,c,mpid)+w(a-1,b-1,c,mpid)+w(a-1,b,c-1,mpid)-w(a-1,b-1,c-1,mpid);
if(flag)printf("%lld-->%lld=%lld %lld %lld:%lld\n",ppid,mpid,a,b,c,arr[a][b][c]);
return arr[a][b][c];
}
int main(){
scanf("%lld%lld%lld",&a,&b,&c);
printf("w(%lld,%lld,%lld) =%lld",a,b,c,w(a,b,c,-1));
return 0;
}
用来解析输出的python程序(AI写的)
import sys
from collections import defaultdict
class CallNode:
__slots__ = ('pid', 'a', 'b', 'c', 'retval', 'is_cache', 'children')
def __init__(self, pid, a, b, c, retval, is_cache=False):
self.pid = pid # 函数调用PID
self.a = a # 参数a
self.b = b # 参数b
self.c = c # 参数c
self.retval = retval # 返回值
self.is_cache = is_cache # 是否为缓存命中
self.children = [] # 子调用列表
def parse_line(line):
try:
if 'cache:' in line:
parts = line.split('cache:')
if len(parts) < 2:
return None
call_part = parts[0].strip()
values_part = parts[1].strip()
if '-->' not in call_part:
return None
caller, callee = call_part.split('-->')
params_ret = values_part.split(':', 1)
if len(params_ret) < 2:
return None
params = params_ret[0].split()
retval = params_ret[1].strip()
if len(params) != 3:
return None
caller = int(caller.strip())
callee = int(callee.strip())
a, b, c = map(int, params)
retval = int(retval)
return caller, callee, a, b, c, retval, True
else:
caller_part, rest = line.strip().split('-->', 1)
callee_part, values_part = rest.split('=', 1)
params_ret = values_part.split(':', 1)
if len(params_ret) < 2:
return None
params = params_ret[0].split()
retval = params_ret[1].strip()
if len(params) != 3:
return None
caller = int(caller_part.strip())
callee = int(callee_part.strip())
a, b, c = map(int, params)
retval = int(retval)
return caller, callee, a, b, c, retval, False
except Exception:
return None
def print_tree(node, depth=0):
indent = ' ' * depth
cache_tag = "[缓存] " if node.is_cache else ""
print(f"{indent}{node.pid}: {cache_tag}w({node.a},{node.b},{node.c}) → {node.retval}")
for child in node.children:
print_tree(child, depth + 1)
def main():
nodes = {} # PID到节点的映射
calls = defaultdict(list) # 调用者PID到被调用节点列表的映射
total_calls = 0 # 总调用次数统计器
cache_hits = 0 # 缓存命中次数统计器
for line in sys.stdin:
parsed = parse_line(line.strip())
if not parsed:
continue
caller, callee, a, b, c, retval, is_cache = parsed
total_calls += 1
if is_cache:
cache_hits += 1
if callee not in nodes:
node = CallNode(callee, a, b, c, retval, is_cache)
nodes[callee] = node
else:
nodes[callee].a = a
nodes[callee].b = b
nodes[callee].c = c
nodes[callee].retval = retval
nodes[callee].is_cache = is_cache
calls[caller].append(nodes[callee])
for caller_pid, callees in calls.items():
if caller_pid in nodes:
for callee_node in callees:
if callee_node.pid != nodes[caller_pid].pid:
nodes[caller_pid].children.append(callee_node)
del calls
root_nodes = []
if -1 in nodes and nodes[-1].children:
root_nodes = nodes[-1].children
else:
children_set = set()
for callees in nodes.values():
for child in callees.children:
children_set.add(child.pid)
for pid, node in nodes.items():
if pid not in children_set and pid != -1:
root_nodes.append(node)
if root_nodes:
print("函数调用树:")
for root in root_nodes:
print_tree(root)
del nodes
if total_calls > 0:
hit_rate = (cache_hits / total_calls) * 100
print(f"\n缓存命中率: {hit_rate:.2f}% ({cache_hits}/{total_calls})")
else:
print("\n未找到任何有效调用")
if __name__ == '__main__':
main()
这里空空如也
有帮助,赞一个