测试环境:
Windows 10 专业版 X64
CPU:intel Core i7-3612QM 2.1GHZ 4核8线程
内存:8G
C++: Visual Studio 2015 / MSVC 14 使用mysql官方驱动 connector/C++ 1.17 ,boost 1.63.0
JAVA: jre 1.8.0_121 使用 mysql官方connector/j 5.1.40 ,jdbc驱动
go: go 1.8,Go没有官方的mysql驱动,使用github.com/Go-SQL-Driver/MySQL
测试:对mysql进行1万条数据插入操作,启用事务
C++ 测试源码:
#include <iostream>
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <Windows.h>
#include <mysql_error.h>
#include <cppconn\statement.h>
using namespace std;
using namespace sql;
int main()
{
mysql::MySQL_Driver* driver;
Connection* con;
Statement* state;
LARGE_INTEGER beginTime;
LARGE_INTEGER endTime;
LARGE_INTEGER freQuency;
driver = mysql::get_mysql_driver_instance();
try {
con = driver->connect("tcp://localhost:3306", "myuser", "123456");
state = con->createStatement();
state->execute("use mytestdb");
QueryPerformanceFrequency(&freQuency);
QueryPerformanceCounter(&beginTime);
SQLString sql = "insert into test_table values(NULL,'xxx','a@a.a','12345678','Promise Land');";
con->setAutoCommit(false);
for (int i = 0; i < 10000; i++)
{
state->execute(sql);
}
con->commit();
QueryPerformanceCounter(&endTime);
con->setAutoCommit(true);
}
catch (SQLException &e)
{
cout << "MySql Error:" << e.what() << endl;
return -1;
}
unsigned long long escp = endTime.QuadPart - beginTime.QuadPart;
cout << "CPU:" << freQuency.QuadPart << endl;
cout << "TimeElapsed: " << escp * 1000 / freQuency.QuadPart << endl;
delete state;
delete con;
return 0;
}
package testmysql;
import java.sql.*;
public class TMysql {
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mytestdb",
"myuser",
"123456");
String sql = "insert into test_table values(NULL,'java','a@a.a','12345678','Promise Land');";
PreparedStatement pstmt = (PreparedStatement)conn.prepareStatement(sql);
long startTime = System.currentTimeMillis();
conn.setAutoCommit(false);
for(int i =0;i<10000;i++)
{
pstmt.execute();
}
conn.commit();
conn.setAutoCommit(true);
long endTime=System.currentTimeMillis();
System.out.println("TimeElapsed: "+String.valueOf(endTime-startTime));
}catch(SQLException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Go测试源码:
package main
import (
"database/sql"
"fmt"
"time"
_ "github.com/Go-SQL-Driver/MySQL"
)
func main() {
db, err := sql.Open("mysql", "myuser:123456@tcp(localhost:3306)/mytestdb")
if err != nil {
fmt.Println("Open Database Error:", err)
}
startTime := time.Now().UnixNano() / 1e6
tx, err := db.Begin()
if err != nil {
fmt.Println("Begin Error:", err)
}
for i := 0; i < 10000; i++ {
stmt, _ := tx.Prepare("insert into test_table values(NULL,'go','a@a.a','12345678','Promise Land');")
stmt.Exec()
}
tx.Commit()
endTime := time.Now().UnixNano() / 1e6
fmt.Println("Time Elapsed:", endTime-startTime)
}
测试10次结果(单位:毫秒)
结论,
性能:C++最快,go次之,不过相差不大,JAVA速度几乎比C++慢了1倍。
易用性:JAVA使用的人最多,且网上文档多,操作方便,GO次之,C++用起来比较麻烦点
前途:go如果官方有驱动支持,应该还是不错的,至少性能没比C++差多少,怎么说也还只是新兴的语言,发展前景广阔。但是当前来说,文档较少,支持不多,不推荐用于生产环境。
对于访问较大的业务,如果不嫌麻烦,可以使用C++。
对于业务逻辑较强,数据库访问相对不大,且要求敏捷开发,还是用JAVA比较好。