123
返回列表 发新帖
楼主: newkid

[转载] (转载)98-老虎刘和chatGPT pk了一把SQL改写优化

[复制链接]
论坛徽章:
520
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
21#
 楼主| 发表于 2023-3-27 22:49 | 只看该作者
could you optimize this PL/SQL block?

declare

  n number := 100 ;
  v number :=0 ;
begin
  for i in 1..n loop
     for j in i..n-i loop
         v:=v+1 ;
     end loop ;
  end loop ;
  DBMS_OUTPUT.PUT_LINE( 'v='||v ) ;
end ;
/

Here's an optimized version of the PL/SQL block you provided:

DECLARE
n NUMBER := 100;
v NUMBER := 0;
BEGIN
v := ((n * (n + 1)) / 2) - ((n / 2) * (n / 2));
DBMS_OUTPUT.PUT_LINE('v=' || v);
END;

The optimized version of the block uses a mathematical formula to calculate the value of v instead of using nested loops. The formula used is:

v = (n * (n + 1) / 2) - (n / 2) * (n / 2)

This formula calculates the number of times the inner loop is executed without actually executing the loop. It is derived from the fact that the sum of the first n natural numbers is given by the formula:

sum = n * (n + 1) / 2

The inner loop in the original code is equivalent to counting the number of integers between i and n-i inclusive. By using the mathematical formula, we can calculate the sum of these integers without iterating over them one by one.

The optimized version is faster and more efficient than the original version because it avoids the nested loop and uses a mathematical formula instead.

使用道具 举报

回复
论坛徽章:
520
奥运会纪念徽章:垒球
日期:2008-09-15 01:28:12生肖徽章2007版:鸡
日期:2008-11-17 23:40:58生肖徽章2007版:马
日期:2008-11-18 05:09:48数据库板块每日发贴之星
日期:2008-11-29 01:01:02数据库板块每日发贴之星
日期:2008-12-05 01:01:03生肖徽章2007版:虎
日期:2008-12-10 07:47:462009新春纪念徽章
日期:2009-01-04 14:52:28数据库板块每日发贴之星
日期:2009-02-08 01:01:03生肖徽章2007版:蛇
日期:2009-03-09 22:18:532009日食纪念
日期:2009-07-22 09:30:00
22#
 楼主| 发表于 2023-3-27 22:58 | 只看该作者
jihuyao 发表于 2023-3-27 10:59
I am also wondering if chatGPT can see some simple hidden rules/patterns, like two shown below (one  ...

你这两个例子看不出什么逻辑来,如果要观察规律,给的例子又太少。
你自己上不了chatgpt吗?

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表