Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

benchmarks calc integral and socket

  • Loading branch information...
commit 54f53a06b041455b2fbd4e1fc98a3af10be228b9 0 parents
@Mikxail authored
40 integral.cpp
@@ -0,0 +1,40 @@
+// #include <math.h>
+#include <stdio.h>
+#include <time.h>
+#include <cmath>
+#include <iostream>
+
+//ôóíêöèÿ, àíàëîãè÷íàÿ òîìó ÷òî ìû äåëàëè íà js
+float func(float x, float y){
+ return sin(x*y)/(1+sqrt(x*x+y*y))+2;
+}
+
+void funcCPU(float x0, float xn, float y0, float yn, int iterations){
+ double x,y,value,result;
+ clock_t t1, t2;
+ time_t z1, z2;
+ result=0;
+ t1 = clock();
+ z1 = time(NULL);
+ for (int i = 0; i < iterations; i++){
+ for (int j = 0; j < iterations; j++){
+ x = x0 + (xn - x0) / iterations * i;
+ y = y0 + (yn - y0) / iterations * j;
+ value = func(x, y);
+ result+=value*(xn-x0)*(yn-y0)/(iterations*iterations);
+ }
+ }
+ //char *c = new char[20];
+ t2 = clock();
+ z2 = time(NULL);
+ printf("C result = %f\n",result);
+ printf("C time = %f\n(%f; %f)\n", ((float)t2 - (float)t1)/1000, (float)t1, (float)t2);
+ printf("%ld; %ld\n", z1, z2);
+ return;
+ //return c;
+}
+
+int main(){
+ funcCPU(-4,4,-4,4,1024);
+ return 0;
+}
21 integral.js
@@ -0,0 +1,21 @@
+var func = function(x,y){
+ return Math.sin(x*y)/(1+Math.sqrt(x*x+y*y))+2;
+}
+function integrateJS(x0,xN,y0,yN,iterations){
+ var result=0;
+ var time = new Date().getTime();
+ for (var i = 0; i < iterations; i++){
+ for (var j = 0; j < iterations; j++){
+ //âû÷èñëåíèå êîîðäèíàò òåêóùåãî ïðÿìîóãîëüíèêà
+ var x = x0 + (xN - x0) / iterations * i;
+ var y = y0 + (yN - y0) / iterations * j;
+ var value = func(x, y); //âû÷èñëåíèå çíà÷åíèÿ ôóíêöèè
+ //âû÷èñëåíèå îáúåìà ïàðàëëåëåïèïåäà è ïðèáàâêà ê îáùåìó îáúåìó
+ result+=value*(xN-x0)*(yN-y0)/(iterations*iterations);
+ }
+ }
+ console.log("JS result = "+result);
+ console.log("JS time = "+(new Date().getTime() - time));
+}
+
+integrateJS(-4,4,-4,4,1024);
25 integral.php
@@ -0,0 +1,25 @@
+<?
+function func ($x,$y){
+ return sin($x*$y)/(1+sqrt($x*$x+$y*$y))+2;
+}
+function integratePHP($x0,$xN,$y0,$yN,$iterations){
+ $result=0;
+ $time = microtime(true);
+ for ($i = 0; $i < $iterations; $i++){
+ for ($j = 0; $j < $iterations; $j++){
+ //âû÷èñëåíèå êîîðäèíàò òåêóùåãî ïðÿìîóãîëüíèêà
+ $x = $x0 + ($xN - $x0) / $iterations * $i;
+ $y = $y0 + ($yN - $y0) / $iterations * $j;
+ $value = func($x, $y); //âû÷èñëåíèå çíà÷åíèÿ ôóíêöèè
+ //âû÷èñëåíèå îáúåìà ïàðàëëåëåïèïåäà è ïðèáàâêà ê îáùåìó îáúåìó
+ $result+=$value*($xN-$x0)*($yN-$y0)/($iterations*$iterations);
+ }
+ }
+ echo ("PHP result = ".$result);
+ echo ("\nPHP time = ".((microtime(true) - $time)*1000));
+ echo "\n";
+}
+
+integratePHP(-4,4,-4,4,1024);
+
+?>
30 integral.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Time::HiRes qw (time);
+
+sub func {
+ my ($x, $y) = @_;
+ return sin($x*$y)/(1+sqrt($x*$x+$y*$y))+2;
+}
+sub integratePerl{
+ my ($x0,$xN,$y0,$yN,$iterations) = @_;
+ my $result=0;
+ my $_time = time();
+ for (my $i = 0; $i < $iterations; $i++){
+ for (my $j = 0; $j < $iterations; $j++){
+ #âû÷èñëåíèå êîîðäèíàò òåêóùåãî ïðÿìîóãîëüíèêà
+ my $x = $x0 + ($xN - $x0) / $iterations * $i;
+ my $y = $y0 + ($yN - $y0) / $iterations * $j;
+ my $value = func($x, $y); #âû÷èñëåíèå çíà÷åíèÿ ôóíêöèè
+ #âû÷èñëåíèå îáúåìà ïàðàëëåëåïèïåäà è ïðèáàâêà ê îáùåìó îáúåìó
+ $result+=$value*($xN-$x0)*($yN-$y0)/($iterations*$iterations);
+ }
+ }
+ print("Perl result = ".$result);
+ print("\nPerl time = ".((time() - $_time)*1000) );
+ print "\n";
+}
+
+integratePerl(-4,4,-4,4,1024);
22 integral_pdl.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+
+
+use strict;
+use PDL;
+use Time::HiRes qw (time);
+#use Benchmark qw(:all :hireswallclock);
+
+#timethis(1, sub { print integrate(-4,4,-4,4,1024)."\n" });
+
+my $_time = time();
+my $result = integrate(-4,4,-4,4,1024);
+print("Perl result = ".$result);
+print("\nPerl time = ".((time() - $_time)*1000) );
+print "\n";
+
+sub integrate {
+ my ($x0,$x1,$y0,$y1,$steps) = @_;
+ my $d = zeroes(float, $steps, $steps);
+ my ($x, $y) = ( $d->xlinvals($x0, $x1), $d->ylinvals($y0, $y1) );
+ sum(sin($x * $y) / (1 + sqrt($x**2 + $y**2)) + 2) * ($x1-$x0) * ($y1-$y0) / $steps**2;
+}
22 server.js
@@ -0,0 +1,22 @@
+#!/usr/bin/env node
+
+var out = "HTTP/1.1 200 Ok\n"
+out += "Content-Type: text/html; charset=UTF-8\n";
+out += "Connection: close\n";
+out += "Content-Length: 1\n\n1";
+
+var net = require('net');
+var server = net.createServer(function(c) { //'connection' listener
+ //console.log('server connected');
+/* c.on('end', function() {
+ console.log('server disconnected');
+ });
+ */
+ c.setNoDelay();
+ c.write(out);
+ c.end();
+ //c.pipe(c);
+});
+server.listen(8888, function() { //'listening' listener
+ console.log('server bound');
+});
57 server.pl
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+
+use IO::Socket;
+use IO::Select;
+
+my %SES;
+
+my $PORT = 8888;
+
+my $out = q~HTTP/1.1 200 Ok
+Content-Type: text/html; charset=UTF-8
+Connection: close
+Content-Length: 1
+
+a~;
+
+my $listen_socket = IO::Socket::INET->new(
+ LocalPort => $PORT,
+ Proto => 'tcp',
+ Listen => 20,
+ ReuseAddr => 1
+) or die $@;
+
+my $readers = IO::Select->new() or die $@;
+$readers->add($listen_socket);
+
+warn "Started";
+
+my (@ready, $handle, $bytes);
+while (1) {
+ @ready = $readers->can_read(0);
+
+ for $handle (@ready) {
+ if ($handle eq $listen_socket) { # accept
+ my $connect = $listen_socket->accept();
+ $readers->add($connect);
+ #syswrite($connect, $out);
+ #print $connect rand();
+ #close $connect;
+ } else {
+ my $input;
+ $bytes = sysread($handle, $input, 1024);
+ if ($bytes > 0) {
+ syswrite($handle, $out);
+ $readers->remove($handle);
+ close $handle;
+ } else {
+ #$readers->remove($handle);
+ #close $handle;
+ }
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.