科特林。Java和PHP性能与MySQL

我一直在对各种语言的MySQL执行性能进行一些测试,以衡量它们的整体执行时间,结果对我来说似乎完全不对劲。


测试的主要目标是查看从数据库表中提取数据列表,将该数据转储到对象数组中,然后将该数据输出到屏幕上需要多少秒。根据我得到的数字,我唯一的假设是,我一定是在Java和Kotlin中做了一些完全错误的事情,否则根据结果,它们的表现要慢得多。这虽然没有意义,因为它们都是编译语言,所以它们的性能应该很容易比较,如果不是PHP的性能。


现在我假设下面的代码中有一些东西导致执行时间比PHP慢得多,并且很好奇是否有人对可能发生的事情有任何输入,或者Java mysql连接器只是比PHP中的PDO连接器慢得多?


PHP: 0.21993803977966


Kotlin: 0.783954305


爪哇: 0.716218516


Java Code


        Long startTime = System.nanoTime();


        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3321/database?user=root&password=pass");


        Statement stmt = conn.createStatement();


        StringBuilder stringBuilder = new StringBuilder();


        ArrayList<Entity> contacts = new ArrayList<Entity>();


        ResultSet  rs = stmt.executeQuery( "SELECT * FROM contacts" );


        while( rs.next() ){

            contacts.add( new Entity( rs.getString("firstname"), rs.getString("lastname") ) );

        }


        for( int i = 0; i < 5; i++ ){

            contacts.forEach( contact -> stringBuilder.append("huzzah ").append(contact.getFirstName()).append(" ").append(contact.getLastName()).append("\n\r"));

        }


        Long endTime = System.nanoTime();

        System.out.print( stringBuilder.toString() );


        Long elapsedTime = ( endTime - startTime );


        double seconds = (double) elapsedTime / 1_000_000_000.0;

        System.out.println( "Total Execution Time: " + seconds );

网络工序代码


<?php


$starttime = microtime(true);


include_once "entity.php";


$dbh = new PDO('mysql:host=127.0.0.1;port=3321;dbname=database', "root", "pass");

$query = $dbh->prepare("SELECT * FROM contacts");

$query->execute();


/** @var Entity[] $entities */

$entities = [];


while( $row = $query->fetch(PDO::FETCH_ASSOC)){


    $entities[] = new Entity( $row['firstname'], $row['lastname']);

}


$buffer = "";


for( $i = 0; $i < 5; $i++ ){

    foreach ($entities as $entity) {

        $buffer .= "huzzah {$entity->getFirstName()} {$entity->getLastName()}\n\r";

    }

}

湖上湖
浏览 145回答 2
2回答

ibeautiful

在Java中连接比PHP慢得多,所以我在数据库连接之后指定了开始时间。然而,这并不是故事的全部。编译器也没有像@Thomas建议的那样优化代码。我创建了该项目的导出JAR并进行了一些修改。新时代更加合理,将Java的执行时间减少到0.228579051&nbsp; &nbsp; &nbsp; &nbsp; Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3321/database?user=root&password=password");&nbsp; &nbsp; &nbsp; &nbsp; Long startTime = System.nanoTime();&nbsp; &nbsp; &nbsp; &nbsp; Statement stmt = conn.createStatement();&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder stringBuilder = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; ResultSet&nbsp; rs = stmt.executeQuery( "SELECT * FROM contacts" );&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Entity> contacts = new ArrayList<Entity>(rs.getRow());&nbsp; &nbsp; &nbsp; &nbsp; while( rs.next() ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contacts.add( new Entity( rs.getString("firstname"), rs.getString("lastname") ) );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for( int i = 0; i < 5; i++ ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contacts.forEach( contact -> stringBuilder.append("huzzah ").append(contact.getFirstName()).append(" ").append(contact.getLastName()).append("\n\r"));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Long endTime = System.nanoTime();&nbsp; &nbsp; &nbsp; &nbsp; System.out.print( stringBuilder.toString() );&nbsp; &nbsp; &nbsp; &nbsp; Long elapsedTime = ( endTime - startTime );&nbsp; &nbsp; &nbsp; &nbsp; double seconds = (double) elapsedTime / 1_000_000_000.0;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println( "Total Execution Time: " + seconds );

弑天下

您可以尝试:&nbsp;ResultSet&nbsp; rs = stmt.executeQuery( "SELECT * FROM contacts" );&nbsp;ArrayList<Entity> contacts = new ArrayList<Entity>(rs.getRow());&nbsp; &nbsp; while( rs.next() ){&nbsp; &nbsp; &nbsp; &nbsp; contacts.add( new Entity( rs.getString("firstname"), rs.getString("lastname") ) );&nbsp; &nbsp; }这将在一次调用中分配所需的所有存储,而不是以增量方式为每行添加存储。永远不要低估php的性能和功能。WEB 的一半是用 php 开发的,这是有原因的。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java