王剑编程网

分享专业编程知识与实战技巧

AI工具写代码能力对比

作为一名资深码农,面对来势汹汹的AI大模型时代,应该积极拥抱变化。今天我们就来对比一下,当前国内外比较流行的AI大模型工具写代码能力的对比测试。在同样的需求下,对比一下国内外各大模型写代码能力在代码质量和运行效果上的优劣。

本次对比测试的代码需求很简单:

请生成一段shell脚本可以每秒输出当前CPU和内存的使用情况。

这是一个非常经典的代码需求,现在我们一起来看看各大AI大模型工具会输出哪些代码,并执行查看各个代码的输出结果。

一、阿里大模型通义千问

Bash
#!/bin/bash

# monitor_resources.sh - Script to monitor CPU and memory usage every second.

# Function to get CPU and Memory usage.
get_cpu_mem_usage() {
    # Using top command to get the CPU and Memory usage. We are interested in the second line of output which contains summary information.
    cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *$[0-9.]*$%* id.*/\1/" | awk '{print 100 - $1"%"}')
    mem_usage=$(free | grep Mem | awk '{print $3/$2 * 100.0"%"}')
    
    echo "CPU Usage: $cpu_usage"
    echo "Memory Usage: $mem_usage"
}

get_cpu_mem_usage1() {
    # Using top command to get the CPU and Memory usage. We are interested in the second line of output which contains summary information.
    cpu_idle=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *$[0-9.]*$%* id.*/\1/" | awk '{print $1}')
    cpu_usage=$(echo "scale=2; 100 - $cpu_idle" | bc)
    
    mem_usage=$(free | grep Mem | awk '/Mem/ {printf("%.2f\n", $3/$2 * 100)}')
    
    echo "CPU Usage: $cpu_usage%"
    echo "Memory Usage: $mem_usage%"
}

# Infinite loop to keep monitoring.
while true; do
    get_cpu_mem_usage1
    sleep 1
done

代码运行结果:


能正常输出结果,但竟然有报错信息。

二、百度大模型文心一言

Bash
#!/bin/bash

# 无限循环,每秒输出一次CPU和内存使用情况
while true; do
    # 获取top命令的输出,并提取相关信息
    # 使用grep和awk来处理输出
    cpu_info=$(top -bn1 | grep "Cpu(s)" | awk '{print "CPU Usage: " $2 + "%" " " $4 + "%"}')
    mem_info=$(top -bn1 | grep "KiB Mem" | awk '{print "Memory Usage: " $3/$2 * 100.0 "%" " " ($3-$7)/$2 * 100.0 "%"}')

    # 输出CPU和内存使用情况
    echo "$(date +'%Y-%m-%d %H:%M:%S') - $cpu_info - $mem_info"

    # 等待1秒
    sleep 1
done

代码运行结果:

第一次代码结果没有输出资源的百分比信息,需要加问一次:

Bash
#!/bin/bash

# 无限循环,每秒输出一次CPU和内存使用情况
while true; do
    # 获取top命令的输出,并提取CPU使用率
    cpu_usage=$(top -bn1 | grep "Cpu(s)" | \
        awk '{user=int($2 + 0.5); nice=int($4 + 0.5); system=int($6 + 0.5); idle=int($8 + 0.5); \
              total=user+nice+system+idle; \
              cpu_pct=100 - (idle/total * 100); \
              printf "CPU Usage: %.2f%%\n", cpu_pct}')

    # 获取top命令的输出,并提取内存使用率
    mem_usage=$(top -bn1 | grep "Mem:" | \
        awk '{total=int($2); used=int($3); free=int($4); buffers=int($6) if (NR==2); \
              mem_pct=(total-free-buffers)/total * 100; \
              printf "Memory Usage: %.2f%%\n", mem_pct}')

    # 注意:在某些系统上,Mem:行之后可能紧跟着另一行包含buffers和cached的信息,
    # 这里我们假设buffers在Mem:行的同一行(这取决于top命令的输出格式和你的系统配置)。
    # 如果buffers和cached不在同一行,你可能需要调整这个脚本来正确计算内存使用率。

    # 输出CPU和内存使用情况,并添加时间戳
    echo "$(date +'%Y-%m-%d %H:%M:%S') - $cpu_usage - $mem_usage"

    # 等待1秒
    sleep 1
done


三、抖音大模型豆包

Bash
#!/bin/bash
while true; do
    # 获取 CPU 使用率
    cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2 + $4}')
    # 获取内存使用率
    mem_usage=$(free | grep Mem | awk '{printf("%.2f"), $3/$2*100}')
    # 输出当前时间、CPU 使用率和内存使用率
    echo "$(date '+%Y-%m-%d %H:%M:%S') CPU Usage: $cpu_usage% Memory Usage: $mem_usage%"
    # 休眠 1 秒
    sleep 1
done



四、谷歌出品大模型Gemini

Bash
#!/bin/bash

# 设置输出间隔(单位:秒)
INTERVAL=1

while true; do
  # 获取CPU使用率
  CPU_USAGE=$(top -bn1 | grep 'Cpu(s)' | awk '{print $2 + $4}' | cut -d',' -f1)

  # 获取内存使用率
  MEM_USAGE=$(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2}')

  # 输出结果
  echo "$(date +"%Y-%m-%d %H:%M:%S") CPU使用率:${CPU_USAGE}%,内存使用率:${MEM_USAGE}%"

  # 暂停一段时间
  sleep $INTERVAL
done


五、大模型鼻祖ChatGPT

Bash
#!/bin/bash

# 输出标题
printf "%-10s %-10s\n" "CPU(%)" "Memory(%)"

# 无限循环,每秒更新一次
while true; do
    # 获取CPU使用率
    CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | \
                awk '{print 100 - $8}')

    # 获取内存使用率
    MEM_USAGE=$(free | grep Mem | \
                awk '{printf "%.2f", $3/$2 * 100}')

    # 输出当前CPU和内存使用率
    printf "%-10.2f %-10.2f\n" "$CPU_USAGE" "$MEM_USAGE"

    # 每秒更新一次
    sleep 1
done

ChatGPT在生成代码后,会自动拉起一个Web版本的IDE,方便用户对代码进行调整。


对比测试小结

既然是对比测试,必须有个孰胜孰优的结果。从代码输出质量而已,ChatGPT、Gemini和豆包都表现优秀。而从代码运行结果而已,作为一位10年+的码农还是认为ChatGPT比豆包、Gemini还好。此外,ChatGPT在用户体验的细节上更有优势。

总而言之,作为AI大模型的鼻祖,ChatGPT在本次对比测试中稳住了老大地位,而豆包和Gemini也体现出卓越的竞争力。国内的各大大模型也有不错表现,但提升空间巨大。


控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言