资源
- Overview
- 快速入门
- Claude 3 模型卡
- Claude 3.7 系统卡
- 系统状态
- Anthropic 课程
- 提示词库
- 提示词库
- 宇宙键盘
- 企业预见者
- 网站向导
- Excel公式专家
- Google Apps 脚本编写者
- Python 错误修复器
- 时间旅行顾问
- 讲故事助手
- 引用你的来源
- SQL 魔法师
- 解梦师
- 双关语大师
- 烹饪创作者
- 混合词诗人
- 幽默助手哈尔
- LaTeX 指南
- 情绪色彩转换器
- Git 精通
- 比喻大师
- 伦理困境导航器
- 会议记录员
- 成语解析器
- 代码顾问
- 函数制造者
- 新词创造者
- CSV 转换器
- 表情符号编码器
- 文章润色师
- 观点权衡者
- 琐事生成器
- 正念导师
- 二年级简化器
- VR健身创新者
- PII 净化器
- 备忘录大师
- 职业教练
- 评分大师
- 绕口令
- 面试问题设计师
- 语法精灵
- 猜谜语
- 代码解释器
- 外星人类学家
- 数据整理器
- 品牌构建者
- 效率估算器
- 评论分类器
- 方向解码器
- 激励缪斯
- 电子邮件提取器
- 专业审核员
- 课程计划制定者
- 苏格拉底式智者
- 头韵炼金术师
- 未来主义时尚顾问
- 多语言超能力
- 产品命名专家
- 哲学沉思
- 电子表格魔术师
- 科幻场景模拟器
- 自适应编辑器
- 巴别塔的广播
- 推文语调检测器
- 机场代码分析器
提示词库
代码顾问
建议改进以优化Python代码性能。
将此提示复制到我们的开发者控制台中,亲自尝试一下!
内容 | |
---|---|
System | 你的任务是分析提供的Python代码片段并建议改进以优化其性能。识别代码可以变得更高效、更快速或更少资源密集的区域。提供具体的优化建议,并解释这些更改如何能够提升代码的性能。优化后的代码应保持与原始代码相同的功能,同时展示改进的效率。 |
User | def fibonacci(n): if n <= 0: return [] elif n == 1: return [0] elif n == 2: return [0, 1] else: fib = [0, 1] for i in range(2, n): fib.append(fib[i-1] + fib[i-2]) return fib |
示例输出
Python
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
yield a
a, b = b, a + b
解释:使用带有yield的生成器函数对于生成斐波那契序列更加内存高效。它避免了在列表中创建和存储整个序列。相反,它根据需要即时生成每个数字。
API请求
import anthropic
client = anthropic.Anthropic( # 默认为os.environ.get("ANTHROPIC_API_KEY")
api_key="my_api_key",
)
message = client.messages.create(
model="claude-opus-4-20250514",
max_tokens=1000,
temperature=0,
system="Your task is to analyze the provided Python code snippet and suggest improvements to optimize its performance. Identify areas where the code can be made more efficient, faster, or less resource-intensive. Provide specific suggestions for optimization, along with explanations of how these changes can enhance the code's performance. The optimized code should maintain the same functionality as the original code while demonstrating improved efficiency.",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "def fibonacci(n):\n if n <= 0:\n return []\n elif n == 1:\n return [0]\n elif n == 2:\n return [0, 1]\n else:\n fib = [0, 1]\n for i in range(2, n):\n fib.append(fib[i-1] + fib[i-2])\n return fib",
}
],
}
],
)
print(message.content)
import anthropic
client = anthropic.Anthropic( # 默认为os.environ.get("ANTHROPIC_API_KEY")
api_key="my_api_key",
)
message = client.messages.create(
model="claude-opus-4-20250514",
max_tokens=1000,
temperature=0,
system="Your task is to analyze the provided Python code snippet and suggest improvements to optimize its performance. Identify areas where the code can be made more efficient, faster, or less resource-intensive. Provide specific suggestions for optimization, along with explanations of how these changes can enhance the code's performance. The optimized code should maintain the same functionality as the original code while demonstrating improved efficiency.",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "def fibonacci(n):\n if n <= 0:\n return []\n elif n == 1:\n return [0]\n elif n == 2:\n return [0, 1]\n else:\n fib = [0, 1]\n for i in range(2, n):\n fib.append(fib[i-1] + fib[i-2])\n return fib",
}
],
}
],
)
print(message.content)
import Anthropic from "@anthropic-ai/sdk";
const anthropic = new Anthropic({
apiKey: "my_api_key", // 默认为process.env["ANTHROPIC_API_KEY"]
});
const msg = await anthropic.messages.create({
model: "claude-opus-4-20250514",
max_tokens: 1000,
temperature: 0,
system: "Your task is to analyze the provided Python code snippet and suggest improvements to optimize its performance. Identify areas where the code can be made more efficient, faster, or less resource-intensive. Provide specific suggestions for optimization, along with explanations of how these changes can enhance the code's performance. The optimized code should maintain the same functionality as the original code while demonstrating improved efficiency.",
messages: [
{
"role": "user",
"content": [
{
"type": "text",
"text": "def fibonacci(n):\n if n <= 0:\n return []\n elif n == 1:\n return [0]\n elif n == 2:\n return [0, 1]\n else:\n fib = [0, 1]\n for i in range(2, n):\n fib.append(fib[i-1] + fib[i-2])\n return fib"
}
]
}
]
});
console.log(msg);
from anthropic import AnthropicBedrock
# 有关身份验证选项,请参阅https://docs.anthropic.com/claude/reference/claude-on-amazon-bedrock
client = AnthropicBedrock()
message = client.messages.create(
model="anthropic.claude-opus-4-20250514-v1:0",
max_tokens=1000,
temperature=0,
system="Your task is to analyze the provided Python code snippet and suggest improvements to optimize its performance. Identify areas where the code can be made more efficient, faster, or less resource-intensive. Provide specific suggestions for optimization, along with explanations of how these changes can enhance the code's performance. The optimized code should maintain the same functionality as the original code while demonstrating improved efficiency.",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "def fibonacci(n):\n if n <= 0:\n return []\n elif n == 1:\n return [0]\n elif n == 2:\n return [0, 1]\n else:\n fib = [0, 1]\n for i in range(2, n):\n fib.append(fib[i-1] + fib[i-2])\n return fib"
}
]
}
]
)
print(message.content)
import AnthropicBedrock from "@anthropic-ai/bedrock-sdk";
// 有关身份验证选项,请参阅https://docs.anthropic.com/claude/reference/claude-on-amazon-bedrock
const client = new AnthropicBedrock();
const msg = await client.messages.create({
model: "anthropic.claude-opus-4-20250514-v1:0",
max_tokens: 1000,
temperature: 0,
system: "Your task is to analyze the provided Python code snippet and suggest improvements to optimize its performance. Identify areas where the code can be made more efficient, faster, or less resource-intensive. Provide specific suggestions for optimization, along with explanations of how these changes can enhance the code's performance. The optimized code should maintain the same functionality as the original code while demonstrating improved efficiency.",
messages: [
{
"role": "user",
"content": [
{
"type": "text",
"text": "def fibonacci(n):\n if n <= 0:\n return []\n elif n == 1:\n return [0]\n elif n == 2:\n return [0, 1]\n else:\n fib = [0, 1]\n for i in range(2, n):\n fib.append(fib[i-1] + fib[i-2])\n return fib"
}
]
}
]
});
console.log(msg);
from anthropic import AnthropicVertex
client = AnthropicVertex()
message = client.messages.create(
model="claude-3-7-sonnet-v1@20250219",
max_tokens=1000,
temperature=0,
system="Your task is to analyze the provided Python code snippet and suggest improvements to optimize its performance. Identify areas where the code can be made more efficient, faster, or less resource-intensive. Provide specific suggestions for optimization, along with explanations of how these changes can enhance the code's performance. The optimized code should maintain the same functionality as the original code while demonstrating improved efficiency.",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "def fibonacci(n):\n if n <= 0:\n return []\n elif n == 1:\n return [0]\n elif n == 2:\n return [0, 1]\n else:\n fib = [0, 1]\n for i in range(2, n):\n fib.append(fib[i-1] + fib[i-2])\n return fib"
}
]
}
]
)
print(message.content)
import { AnthropicVertex } from '@anthropic-ai/vertex-sdk';
// 从`CLOUD_ML_REGION`和`ANTHROPIC_VERTEX_PROJECT_ID`环境变量读取。
// 此外,还通过标准的`google-auth-library`流程。
const client = new AnthropicVertex();
const msg = await client.messages.create({
model: "claude-3-7-sonnet-v1@20250219",
max_tokens: 1000,
temperature: 0,
system: "Your task is to analyze the provided Python code snippet and suggest improvements to optimize its performance. Identify areas where the code can be made more efficient, faster, or less resource-intensive. Provide specific suggestions for optimization, along with explanations of how these changes can enhance the code's performance. The optimized code should maintain the same functionality as the original code while demonstrating improved efficiency.",
messages: [
{
"role": "user",
"content": [
{
"type": "text",
"text": "def fibonacci(n):\n if n <= 0:\n return []\n elif n == 1:\n return [0]\n elif n == 2:\n return [0, 1]\n else:\n fib = [0, 1]\n for i in range(2, n):\n fib.append(fib[i-1] + fib[i-2])\n return fib"
}
]
}
]
});
console.log(msg);