I am studying why my C-test program (libmysql) is almost twice as slow as the same php-test (cli) program:
I create a prepared statement and insert 10 records.
php-cli-version (mysqlnd) is almost twice as fast as version C using libmysql. I test like this: time. / dbctest && & timephp phptest.php
C-Version:
#include <my_global.h>
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char my_str[16];
int my_str_length=0;
static void insert_records (MYSQL_STMT *stmt)
{
char *stmt_str = "insert into bleach values (?)";
MYSQL_BIND param[1];
if (mysql_stmt_prepare (stmt, stmt_str, strlen (stmt_str)) != 0)
{
fprintf(stderr,"\nCant prepare...");
return;
}
memset ((void *) param, 0, sizeof (param));
strcpy (my_str,"test");
my_str[5] = '\0';
my_str_length = strlen (my_str);
param[0].buffer_type = MYSQL_TYPE_STRING;
param[0].buffer = (void *) my_str;
param[0].buffer_length = sizeof (my_str);
param[0].is_null = 0;
if (mysql_stmt_bind_param (stmt, param) != 0)
{
fprintf(stderr,"\nCant bind..");
return;
}
for (int i = 1; i < 10; i++)
{
if (mysql_stmt_execute (stmt) != 0)
{
fprintf(stderr,"Error inserting row");
return;
}
}
}
int main (int argc, char *argv[])
{
MYSQL *conn;
conn = mysql_init(NULL);
if (conn == NULL)
{
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
if (mysql_real_connect(conn, "127.0.0.1", "root","password", "test_db", 0, NULL, 0) == NULL)
{
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
MYSQL_STMT *stmt;
stmt = mysql_stmt_init (conn);
insert_records(stmt);
exit (0);
}
PHP versions:
<?php
$mysqli = new mysqli("127.0.0.1", "root", "password", "test_db");
$name = "TEST-PHP";
$stmt = $mysqli->prepare("insert into bleach values (?)");
$stmt->bind_param('s', $name);
for($i=0;$i< 10;$i++)
{
$stmt->execute();
}
$mysqli->close();
?>
source
share