Trong quá trình phát triển phần mềm, hiệu suất code luôn là một yếu tố then chốt, ảnh hưởng trực tiếp đến trải nghiệm người dùng và khả năng mở rộng của hệ thống. Tuy nhiên, việc tối ưu hóa không phải lúc nào cũng đơn giản, đặc biệt khi code ngày càng phức tạp và yêu cầu xử lý ngày càng cao.
Sự xuất hiện của các mô hình AI đã mang đến một cách tiếp cận mới: Sử dụng prompt được thiết kế hợp lý để phân tích, phát hiện điểm nghẽn và đề xuất cải tiến hiệu suất một cách nhanh chóng.
Vì vậy, việc xây dựng các mẫu prompt tối ưu hóa hiệu suất code không chỉ giúp tận dụng tối đa sức mạnh của AI mà còn hỗ trợ lập trình viên nâng cao chất lượng và hiệu quả của sản phẩm phần mềm.
Prompt tối ưu hóa hiệu suất code
Điều chỉnh hiệu suất
Phân tích đoạn code này để tìm kiếm các cơ hội tối ưu hóa hiệu suất:
[DÁN CODE]
Các vấn đề hiện tại: [MÔ TẢ CÁC VẤN ĐỀ VỀ HIỆU SUẤT]
Các ràng buộc: [ví dụ: Giới hạn bộ nhớ, Yêu cầu về thời gian phản hồi]
Đề xuất:
- Cải tiến thuật toán
- Tối ưu hóa cấu trúc dữ liệu
- Chiến lược lưu cache
- Truy vấn cơ sở dữ liệu tối ưu hóa
- Phân tích độ phức tạp thời gian/không gianPhù hợp nhất cho: GPT-5, Claude 4 Sonnet
Kết quả tham khảo thực hiện bằng GPT-5
Phân tích đoạn code này để tìm kiếm các cơ hội tối ưu hóa hiệu suất:
# models.py
from django.db import models
from django.contrib.auth.models import User
class Product(models.Model):
name = models.CharField(max_length=255)
price = models.FloatField()
class Order(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
class OrderItem(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.IntegerField()
# views.py
from django.http import JsonResponse
from django.contrib.auth.models import User
from .models import Product, Order, OrderItem
def report(request):
result = []
# Lấy toàn bộ user (không phân trang)
users = User.objects.all()
for user in users:
user_orders = Order.objects.filter(user=user)
total_spent = 0
total_items = 0
for order in user_orders:
items = OrderItem.objects.filter(order=order)
for item in items:
# Query lại product mỗi lần (N+1)
product = Product.objects.get(id=item.product.id)
total_spent += product.price * item.quantity
total_items += item.quantity
# Lấy top sản phẩm user mua (logic Python thay vì DB)
product_count = {}
for order in user_orders:
items = OrderItem.objects.filter(order=order)
for item in items:
name = item.product.name
product_count[name] = product_count.get(name, 0) + item.quantity
top_product = None
if product_count:
top_product = max(product_count, key=product_count.get)
result.append({
"username": user.username,
"total_spent": total_spent,
"total_items": total_items,
"top_product": top_product
})
return JsonResponse({"data": result})
Các vấn đề hiện tại:
❌ N+1 queries nghiêm trọng:
Query Order theo từng user
Query OrderItem theo từng order
Query Product trong vòng lặp
❌ Query lặp lại nhiều lần:
OrderItem.objects.filter(order=order) bị gọi lại nhiều lần
❌ Không dùng ORM hiệu quả:
Không dùng select_related, prefetch_related
Không dùng annotate, aggregate
❌ Xử lý nặng ở Python:
Tính tổng tiền, top product bằng loop thay vì DB
❌ Không phân trang / không giới hạn dữ liệu
❌ Thiếu caching cho dữ liệu report
Các ràng buộc: Yêu cầu về thời gian phản hồi: 1 giây
Đề xuất:
- Cải tiến thuật toán
- Tối ưu hóa cấu trúc dữ liệu
- Chiến lược lưu cache
- Truy vấn cơ sở dữ liệu tối ưu hóa
- Phân tích độ phức tạp thời gian/không gian











Làm chủ AI
Học IT










AI
Hàm Excel