Comparison of sequencing efficiency between table-built-in-order and c/c++/java/php/-in-order algorithm in LUA

Source: Internet
Author: User
Tags lua

Lua's scripting language makes it easy to handle business logic as a configuration file, but in a lot of places where it needs to be done, it's a little less than a quick-sort algorithm that compares the LUA built-in sorting algorithm to the C + + PHP java.

The fast sorting algorithm is based on the bubbling sort, which is optimized for the time complexity T (n) =o (nlog2n), and the internal use of the two-point strategy.

It is found that the direct operation efficiency under the Luaide LDT is more efficient than the 500W data ordering by using the C + + load to run the Lua script, the script is as follows

The same sort script the LUA interpreter's built-in sorting algorithm runs faster than 14 times times faster than the LUA interpreter calls through C/

The fast sort speed of C/s + + is more than 40 times times the speed of the LUA inline sorting algorithm, which is 3 times times more than the Lua sort in the LDT, at least what I see on my computer is this effect.

A fast sorting algorithm for PHP implementation

PHP simply do not dare to use it to sort, the page cache pool capacity is too small, 5W data sorting is already unbearable .... It's far less efficient than LUA, so just use it as a web crud. Even if you configure the page memory pool size to meet the requirements of 5000 it takes close to 2s

~ ~ ~ still enclose the code.

<?php   define (' array_size ',);      function QuickSort ($arr, $low, $high)   {     if ($low > $high)   return; $begin = $low; $end = $high; $key = $arr [$ begin];     while ($begin < $end) {    while ($begin < $end && $arr [$end]>= $key)-   -$end; $arr [$begin]= $arr [$ end];        while ($begin < $end && $arr [$begin]<= $key)  + + $begin; $arr [$end]= $arr [$begin];}  $arr [$begin]= $key;      QuickSort ($arr, $low, $begin-1);  QuickSort ($arr, $begin +1, $high);   }   $arr =array ();   Echo ' before insertion: '. Time (). ' <br/> ';   for ($i =0; $i <ARRAY_SIZE; $i + +)   {     Array_push ($arr, rand (1,50000));   }   Echo ' after insertion: '. Time (). ' <br/> ';   Echo ' before sorting: '. Time (). ' <br/> ';   QuickSort ($arr, 0,array_size-1);   Echo ' sorted after: '. Time (). ' <br/> ';? >



java Real Fast sorting algorithm if it is not because the Java Big Data processing JVM is prone to stack overflow, then the efficiency of the flat    even better than that of C + + the JVM is doing very nicely    according to one eldest brother said Yes just in  Time compile Technology like. NET   Small segment code execution is particularly efficient, and large programs are not.

In the Java programming language and environment, the instant compiler (JIT Compiler,just-in-time compiler) is a program that translates Java bytecode (including programs that need to be interpreted) into instructions that can be sent directly to the processor (processor). When you have written a Java program, the source language's statements will be compiled into bytecode by the Java compiler, rather than compiled into a directive code corresponding to a particular processor hardware platform (for example, Intel's Pentium microprocessor or IBM's system/390 processor). Bytecode is platform-independent code that can be sent to any platform and can be run on that platform.
In the past, most programs written in any language had to be recompiled on every computer platform, and sometimes even needed to be rewritten. One of the biggest advantages of Java is that you only need to write and compile the program once. On any platform, Java interprets compiled bytecode as instructions that can be understood by a particular processor. However, the Java virtual machine can only process one bytecode instruction at a time. Using the Java Instant compiler (actually the second compiler) on a particular system platform can compile bytecode into specific system code (although the program was originally compiled on this platform). Once the code is compiled by the JIT compiler (heavy), it usually runs faster on the computer.
  
The instant compiler (JIT compiler) is supplied with the virtual machine and is optionally used. It compiles bytecode into executable code for the specified platform that can be executed immediately. Sun Microsystems recommends that choosing the JIT compiler option usually makes the program run faster, especially if an executable method is reused

Print ("Time before Insertion", Os.clock ())--Defines the table variable sortable={}  for i=1,5000000,1 do  sortable[i]=iendprint (' time after inserting data: ', Os.clock ()) print (' Plugged in ', #sorTable, ' Data! ') Print (' Pre-sort time: ', Os.clock ()) Table.sort (Sortable,function (A, B)  if (a<b) then     return true  else   return false  EndEnd) print ("After sorting time:", Os.clock ())

As the above code in the LDT directly run the following results show that 6s sorted the data of the 500W array, but the same LUA script embedded in the C + + interpreter to explain the effectiveness of the implementation of the lack of terrible


after running the Lua script from Lua C + +, the results are as follows: It takes more than 80S to insert and sort slowly, so it's not worth the use of C + + to load LUA for similar operations . the same LUA script is interpreted by the C + + inline interpretation of the call speed so slow, and then look at the c++php in Java in the efficiency of the sorting algorithm.


It's definitely an advantage for a C/s + + processing of large amounts of data, and the following is the use of a fast sort algorithm for 500W data content ordering, which takes only 2s of time to be three times times the LDT, and is 40 times times more than the Lua script loaded by C/s + +.

#include "stdafx.h" #include "time.h" #include "stdlib.h" #include "stdio.h" #define arrsize 5000000//Quick sort//with key The idea of recursive sequencing comes from the bubble sorting algorithm///The data in two halves of the key is larger than key and the data is recursive to sort//time complexity is nlog2nvoid QuickSort (int arr[],int low,int hi GH) {if (low>=high) return; int begin=low;//Record The starting point of the current call Indexint End=high;//Record the current end point Indexint key=arr[begin];//Use this as the Axis division while (Begin<end) {while (Begin<end&&arr[end]>=key)//Find a small value from the right end--;arr[begin]=arr[end];while ( Begin<end&&arr[begin]<=key)//from the left to look for a large value begin++; Arr[end]=arr[begin];} Arr[begin]=key;  Finally, the key is placed in the remaining position QuickSort (arr,low,begin-1);  Left recursive quicksort (Arr,begin+1,high); Right recursive}int _tmain (int argc,char*argv[]) {int *parray=new int[arrsize];time_t TM; time (&AMP;TM);p rintf ("before insertion:%d\n" , TM), Srand ((unsigned int) time (NULL)), for (int i=0;i<arrsize;i++) {Parray[i]=rand ();} Time (&AMP;TM);p rintf ("After insertion:%d\n", TM), Time (&AMP;TM);p rintf ("Before Sorting:%d\n", TM); QuickSort (parray,0,arrsize-1); time (&AMP;TM);p rintf ("sorted after:%d\n", TM);delete []parray;return 0;} 

running results can be imagined, the time-consuming is almost 2s for C + +, so it is not advisable for LUA to handle big data operations for this kind of scripting language.

Based on Java's fast sorting algorithm, I can't believe my own eyes. The same fast sort algorithm that runs on the JVM Java incredibly faster than C + + 500W sorting data took less than 2s of the time ..... .....????

Import java.lang.String; Import Java.util.date;import Java.util.random;public class QuickSort {public static final int max_size=5000000;//instance initialization { This.a=new int[max_size];///system.out.println ("time before insertion:" +new date (). GetTime () + "MS"); Random rm=new random (+); for (int i=0;i<quicksort.max_size;i++) {this.a[i]=rm.nextint (50000);//Random number} SYSTEM.OUT.PRINTLN ("After insertion time:" +new date (). GetTime () + "MS"); System.out.println ("Time before sorting:" +new date (). GetTime () + "MS"); Quicksortmethod (this.a,0,this.a.length-1); System.out.println ("Sorted Time:" +new date (). GetTime () + "MS");//system.out.println (Arrays.tostring (THIS.A));} The fast sort algorithm Java implementation is the same as C + + public void Quicksortmethod (int[] arr,int Low,int high) {if (low>=high) return; int Begin=low;int End=high;int Key=arr[begin]; Key while (Begin<end) {while (Begin<end&&arr[end]>=key) {--end;} Arr[begin]=arr[end];while (Begin<end&&arr[begin]<=key) {++begin;} Arr[end]=arr[begin];} Arr[begin]=key; Quicksortmethod (arr,low,begin-1); Quicksortmethod (Arr,begin+1,high);}    Private int[] A; public static void Main (STRING[]ARGV) {new QuickSort ();}}




QQ 4223665 Technology Exchange Group

  group number is 387761601  

Welcome to share the software technology together












Comparison of sorting efficiency of table built-in sorting and c/c++/java/php/in Lua

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.