Jump to content

Recommended Posts

what can i do to improve this code?

 

it takes 3 minutes to execute

 

<?php

echo date('l dS \of F Y h:i:s A');
$x= file ('C:\Documents and Settings\Administrator\Desktop\php\tickers.txt');
$fp=fopen('C:\Documents and Settings\Administrator\Desktop\php\test2.txt', 'w+');
$nrRanduri = count ($x);


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

$arr=explode("	",$x[$i]);

$Tran[$i]=$arr[0];  // modific  sau  tranzact 
//$tranzactieEfectuata=$arr[0];
$Piata[$i]=$arr[1];  // piata
$Simbol[$i]=$arr[2];  // simbol 
//$simbol=$arr[2];
//$j[$i][3]=$arr[3];  // bid  vol
//$j[$i][4]=$arr[4];  // best  bid
//$j[$i][5]=$arr[5];  // best  ask
//$j[$i][6]=$arr[6];  // bid  ask
$VolTran[$i]=$arr[7];  //  vol  tranz

$PretTran[$i]=$arr[8];  //  pretul  tranzact
//$pretulTranzactiei=$arr[8];
//$j[$i][9]=$arr[9];  // nr  tranzactii
$DataSiOra[$i]=$arr[10]; // data  s i ora
//$j[$i][11]=$arr[11]; // variatia

$ExplodeDataSiOra=explode(" ",$DataSiOra[$i]);

$Data[$i]=$ExplodeDataSiOra[0];
$Ora[$i]=$ExplodeDataSiOra[1];

$ExplodeData=explode("-",$Data[$i]);
$A[$i]=$ExplodeData[0];
$L[$i]=$ExplodeData[1];
$D[$i]=$ExplodeData[2];

$ExplodeOra=explode(":",$Ora[$i]);
$Or[$i]=$ExplodeOra[0];
$Min[$i]=$ExplodeOra[1];
$Sec[$i]=$ExplodeOra[2];


}

$simbolCautat[0]='SIF1';
$simbolCautat[1]='SIF2';
$simbolCautat[2]='SIF3';
$simbolCautat[3]='SIF4';
$simbolCautat[4]='SIF5';
$simbolCautat[5]='SNP';
$simbolCautat[6]='TLV';
$simbolCautat[7]='BRD';
$simbolCautat[8]='RRC';
$simbolCautat[9]='IMP';
$simbolCautat[10]='TEL';
$simbolCautat[11]='BIO';
$simbolCautat[12]='BRK';
$simbolCautat[13]='ATB';
$simbolCautat[14]='TBM';

$cifre=1000;
for ($i=0;$i<255;$i++)
{
$timp[$i]=$cifre+$i;

if (($i==59) || ($i==119) || ($i==179) || ($i==239))
$cifre=$cifre+40;

}


$nrRanduriSimbolCautat=count ($simbolCautat);
$nrRanduritimp=count ($timp);

for ($j=0;$j<$nrRanduriSimbolCautat;$j++)
{
for ($k=0;$k<$nrRanduritimp;$k++)
{	
	for($i=0;$i<$nrRanduri;$i++)
	{
		if ( ($Tran[$i]==1)&&($Simbol[$i]==$simbolCautat[$j])&&($timp[$k]=="$Or[$i]$Min[$i]") )
		{

		$Pret[$j][$timp[$k]][]=$PretTran[$i];
		$volMoment=$VolTran[$i];
		$volMoment=str_replace(',', '', $volMoment);
		$Volum[$j][$timp[$k]][]=$volMoment;
		}
	}
}
}

$io="<ticker>,<per>,<date>,<time>,<open>,<high>,<low>,<close>,<volume>,<openint>\n";
fwrite($fp,$io);

for ($a=0;$a<$nrRanduriSimbolCautat;$a++) //pentru toate simbolurile fa continoarea
{

			$h=415;                     //verifica cate elemente are fiecare sir a fiecarui simbol
			for ($b=1000;$b<1000+$h;$b++)
			{
				if (isset ($Pret[$a][$b]))
				{
				$f=count ($Pret[$a][$b]);

					$open=$Pret[$a][$b][0];

					$close=$Pret[$a][$b][$f-1];

					$maxim=max($Pret[$a][$b]);

					$minim=min($Pret[$a][$b]);

					$volumTotal=array_sum($Volum[$a][$b]);

					$k="$b";


					$io="$simbolCautat[$a],I,01/23/2008,$k[0]$k[1]:$k[2]$k[3]:00,$open,$maxim,$minim,$close,$volumTotal,0\n";

					fwrite($fp,$io);

				}

			}

}
echo date('l dS \of F Y h:i:s A');
?>

Link to comment
https://forums.phpfreaks.com/topic/94777-improving-code/
Share on other sites

it takes a file that contains data like this:

 

 

0	RGBS	ARAX	40,000	0.2400	0.2400	6,000	-	0.0000	-	2008-01-23 09:30:01	0
0	REGS	COS	300	6.4000	6.4000	300	-	0.0000	-	2008-01-23 09:30:02	0
0	REGS	SIF3	11,000	1.7100	1.7500	4,200	-	0.0000	-	2008-01-23 09:30:10	0
0	REGS	TLV	28,000	0.7200	0.7200	300	-	0.0000	-	2008-01-23 09:30:15	0
0	REGS	OIL	1,700	0.7250	0.7700	2,800	-	0.0000	-	2008-01-23 09:30:19	0
0	REGS	TLV	28,000	0.7200	0.7000	300	-	0.0000	-	2008-01-23 09:30:20	0
0	REGS	SNP	72,000	0.4080	0.4120	100	-	0.0000	-	2008-01-23 09:30:24	0
0	REGS	TUFE	6,000	0.7500	0.7300	500	-	0.0000	-	2008-01-23 09:30:25	0
0	REGS	BIO	200	0.4490	0.4580	20,000	-	0.0000	-	2008-01-23 09:30:26	0
0	REGS	BRK	2,300	2.0600	2.0600	2,000	-	0.0000	-	2008-01-23 09:30:29	0
0	RGBS	VESY	20,000	0.1580	0.1620	8,000	-	0.0000	-	2008-01-23 09:30:30	0
0	REGS	RRC	12,700	0.0847	0.0830	2,000	-	0.0000	-	2008-01-23 09:30:30	0
0	REGS	SIF4	2,000	1.8100	1.8400	11,900	-	0.0000	-	2008-01-23 09:30:31	0
0	REGS	BRK	5,000	2.0800	2.0600	2,000	-	0.0000	-	2008-01-23 09:30:33	0
0	REGS	SIF3	1,600	1.7600	1.7500	4,200	-	0.0000	-	2008-01-23 09:30:35	0
0	RGBS	ALBZ	600	0.4900	0.4950	10,000	-	0.0000	-	2008-01-23 09:30:36	0
0	REGS	SIF4	1,700	1.8300	1.8400	11,900	-	0.0000	-	2008-01-23 09:30:39	0
0	REGS	SIF1	100	2.6600	2.6000	1,700	-	0.0000	-	2008-01-23 09:30:41	0

 

 

and turn it into something like this:

<ticker>,<per>,<date>,<time>,<open>,<high>,<low>,<close>,<volume>,<openint>
SIF1,I,01/23/2008,10:00:00,2.8000,2.8000,2.7800,2.7800,98500,0
SIF1,I,01/23/2008,10:01:00,2.7800,2.8000,2.7800,2.7800,67000,0
SIF1,I,01/23/2008,10:02:00,2.7800,2.8000,2.7800,2.8000,7500,0
SIF1,I,01/23/2008,10:03:00,2.7900,2.7900,2.7900,2.7900,10000,0
SIF1,I,01/23/2008,10:04:00,2.7900,2.8000,2.7800,2.8000,1200,0
SIF1,I,01/23/2008,10:05:00,2.7900,2.7900,2.7900,2.7900,1000,0
SIF1,I,01/23/2008,10:06:00,2.7800,2.7800,2.7700,2.7700,36900,0
SIF1,I,01/23/2008,10:07:00,2.7700,2.7800,2.7700,2.7800,17400,0
SIF1,I,01/23/2008,10:08:00,2.7400,2.7900,2.7400,2.7800,39923,0
SIF1,I,01/23/2008,10:09:00,2.7700,2.7700,2.7700,2.7700,1900,0
SIF1,I,01/23/2008,10:10:00,2.7700,2.7700,2.7700,2.7700,500,0
SIF1,I,01/23/2008,10:11:00,2.7700,2.7700,2.7700,2.7700,7600,0
SIF1,I,01/23/2008,10:12:00,2.7800,2.7800,2.7800,2.7800,10000,0
SIF1,I,01/23/2008,10:13:00,2.7800,2.7800,2.7800,2.7800,10800,0
SIF1,I,01/23/2008,10:14:00,2.7800,2.7800,2.7200,2.7800,1596,0
SIF1,I,01/23/2008,10:15:00,2.7400,2.7800,2.7400,2.7800,323,0
SIF1,I,01/23/2008,10:16:00,2.7800,2.7800,2.7700,2.7700,8500,0
SIF1,I,01/23/2008,10:17:00,2.7700,2.7700,2.7600,2.7600,1600,0
SIF1,I,01/23/2008,10:18:00,2.7000,2.7700,2.7000,2.7700,182,0
SIF1,I,01/23/2008,10:19:00,2.7700,2.7700,2.7700,2.7700,6000,0
SIF1,I,01/23/2008,10:20:00,2.7700,2.7700,2.7700,2.7700,7000,0
SIF1,I,01/23/2008,10:21:00,2.7800,2.7800,2.7700,2.7700,7100,0
SIF1,I,01/23/2008,10:23:00,2.7000,2.7000,2.7000,2.7000,6,0
SIF1,I,01/23/2008,10:25:00,2.7600,2.7600,2.7600,2.7600,600,0
SIF1,I,01/23/2008,10:27:00,2.7700,2.7700,2.7700,2.7700,8200,0
SIF1,I,01/23/2008,10:28:00,2.7800,2.7800,2.7700,2.7700,5400,0
SIF1,I,01/23/2008,10:29:00,2.7700,2.7700,2.7600,2.7600,500,0
SIF1,I,01/23/2008,10:31:00,2.7600,2.7600,2.7600,2.7600,1000,0
SIF1,I,01/23/2008,10:32:00,2.7200,2.7300,2.7200,2.7300,42,0
SIF1,I,01/23/2008,10:34:00,2.7700,2.7700,2.7700,2.7700,200,0
SIF1,I,01/23/2008,10:35:00,2.7700,2.7700,2.7700,2.7700,400,0
SIF1,I,01/23/2008,10:37:00,2.7700,2.7700,2.7700,2.7700,10000,0

 

those are just the first lines

 

 

Link to comment
https://forums.phpfreaks.com/topic/94777-improving-code/#findComment-485364
Share on other sites

Information overload!. I cannot seem to make any correlation between the two files. You'll need to explain clearly how:

0	RGBS	ARAX	40,000	0.2400	0.2400	6,000	-	0.0000	-	2008-01-23 09:30:01	0

gets converted to:

SIF1,I,01/23/2008,10:00:00,2.8000,2.8000,2.7800,2.7800,98500,0

Link to comment
https://forums.phpfreaks.com/topic/94777-improving-code/#findComment-485367
Share on other sites

Well, I've taken a cursory look at the code and you have a lot of nested loops. Since there are not helpful comments in the code I can't determine if those are absolutely necessary or if there is a better approach. I'm not going to read the code line by line to try and figure out what each line is trying to accomplish. Can you explain how you get from the input to the output?

 

You could also put in some debuggin to see how much time each process is taking. Create a start time at the beginning and then echo the (current time - start time) at key points. That will pinpoint the problem areas.

 

It may not make much difference in performance, but I notice you are setting variables according to the number of items in an array and then doing a for loop using that value. I'd suggest using a foreach loop instead.

 

EDIT: Sorry, you do have comments, they just aren't in my language. They probably are useful to you which is the most important.

Link to comment
https://forums.phpfreaks.com/topic/94777-improving-code/#findComment-485368
Share on other sites

i the file that needs to be modified are a lot of simbols,

 

one of them is ARAX, another one SIF1

 

in every minutes for every simbol are a lot of tranzaction,  i select all the tranzaction for more simbols and for every minute I create a open,maxim,minim,close and a total volume in that minute

 

 

but i just need to look on the code and see if can be inproved

Link to comment
https://forums.phpfreaks.com/topic/94777-improving-code/#findComment-485370
Share on other sites

OK, let me paraphrase so we all understand. You process the input file and create the (open,maxim,minim,close and a total volume) values for each symbol for each minute. Is that correct? Can you identify what the values are in the input file? the data & time are pretty obvious, not sure of the rest.

Link to comment
https://forums.phpfreaks.com/topic/94777-improving-code/#findComment-485373
Share on other sites

1	REGS	SIF1	5,200	2.7300	2.7400	3,300	4,800	2.7300	2	2008-01-23 11:22:18	1.11

 

 

1 if there is one this line will be considered, the 0 are not

REGS omited

 

SIF1 simbol that is search for

...

..

.    omited

4800  volum (this will be aded to the rest of the volume made in this minute)

2.7300 prize, (from wich i select open,close,min,max)

2008-01-23 11:22:18 date and time;

 

11:28:18 is the time, and it will be inserted in an array and then will be created data for the minute 11:28:00 with the help of this row

Link to comment
https://forums.phpfreaks.com/topic/94777-improving-code/#findComment-485378
Share on other sites

here are all the cods from a minute: 11:20:00

0	RGBS	DAFR	2,700	0.3410	0.3420	20,000	-	0.3410	-	2008-01-23 11:20:04	0.00
1	RGBS	VESY	20,000	0.1580	0.1600	2,900	1,000	0.1600	1	2008-01-23 11:20:05	0.00
1	RGBS	ALBZ	-	-	-	-	35,100	0.4930	4	2008-01-23 11:20:06	0.82
1	RGBS	ALBZ	-	-	-	-	100	0.4920	1	2008-01-23 11:20:06	0.61
1	RGBS	ALBZ	800	0.4900	0.4950	26,300	29,800	0.4900	3	2008-01-23 11:20:06	0.20
0	RGBS	DAFR	12,700	0.3410	0.3420	20,000	-	0.3410	-	2008-01-23 11:20:07	0.00
0	REGS	TLV	523,900	0.7250	0.7300	26,400	-	0.7300	-	2008-01-23 11:20:07	1.39
1	REGS	BRD	1,210	20.3000	20.4000	2,270	1,500	20.3000	1	2008-01-23 11:20:09	1.50
0	REGS	SIF5	13,800	3.4800	3.4900	4,100	-	3.4800	-	2008-01-23 11:20:09	0.87
0	REGS	IMP	100	0.3350	0.3360	10,000	-	0.3400	-	2008-01-23 11:20:09	1.80
1	RGBS	PRSN	2,420	21.2000	21.5000	500	1,000	21.5000	1	2008-01-23 11:20:09	-1.83
0	REGS	SIF2	100	2.6900	2.7000	29,400	-	2.6900	-	2008-01-23 11:20:10	1.13
1	RGBS	ARAX	200	0.2390	0.2400	43,600	1,000	0.2400	1	2008-01-23 11:20:11	1.27
1	REGS	BRD	1,160	20.3000	20.4000	2,270	50	20.3000	1	2008-01-23 11:20:11	1.50
1	RGBS	ARAX	200	0.2390	0.2400	40,600	3,000	0.2400	1	2008-01-23 11:20:12	1.27
1	RGBS	PRSN	2,420	21.2000	21.5000	470	30	21.5000	1	2008-01-23 11:20:12	-1.83
0	REGS	BRK	4,400	2.1100	2.1200	1,700	-	2.1200	-	2008-01-23 11:20:13	3.41
0	REGS	SIF1	4,100	2.7200	2.7300	4,000	-	2.7400	-	2008-01-23 11:20:14	1.48
0	REGS	BRK	4,400	2.1100	2.1200	1,800	-	2.1200	-	2008-01-23 11:20:21	3.41
0	REGS	SIF3	38,000	1.7500	1.7700	300	-	1.7500	-	2008-01-23 11:20:22	0.00
1	REGS	SIF4	9,200	1.8200	1.8300	10,100	1,300	1.8200	2	2008-01-23 11:20:22	-0.55
0	REGS	BRD	1,260	20.3000	20.4000	2,270	-	20.3000	-	2008-01-23 11:20:23	1.50
0	ODBS	ARTE	0	0.0000	11.2000	5	-	11.0000	-	2008-01-23 11:20:24	0.00
1	REGS	SIF1	1,400	2.7200	2.7300	4,000	2,700	2.7200	2	2008-01-23 11:20:25	0.74
1	REGS	SIF2	1,100	2.6800	2.6900	19,900	100	2.6900	1	2008-01-23 11:20:27	1.13
1	XMBS	FERO	200	0.5500	0.6000	262	321	0.6000	2	2008-01-23 11:20:31	0.00
1	REGS	TLV	523,900	0.7250	0.7300	25,900	500	0.7300	1	2008-01-23 11:20:31	1.39
1	REGS	BIO	13,000	0.4480	0.4490	71,500	8,000	0.4480	1	2008-01-23 11:20:32	-0.22
1	REGS	OLT	2,700	0.9950	1.0000	500	1,000	1.0100	1	2008-01-23 11:20:32	2.02
0	REGS	TLV	522,900	0.7250	0.7300	25,900	-	0.7300	-	2008-01-23 11:20:32	1.39
0	REGS	SIF2	1,100	2.6800	2.6900	26,900	-	2.6900	-	2008-01-23 11:20:33	1.13
1	REGS	RRC	-	-	-	-	67,000	0.0831	2	2008-01-23 11:20:36	-1.89
1	REGS	RRC	50,000	0.0826	0.0830	651,000	62,000	0.0830	2	2008-01-23 11:20:36	-2.01
0	REGS	BRK	4,400	2.1100	2.1200	5,800	-	2.1200	-	2008-01-23 11:20:37	3.41
1	XMBS	SCTB	5	29.0000	30.0000	59	1	30.0000	1	2008-01-23 11:20:39	7.91
1	UNLS	NVL	550,900	0.0300	0.0400	734,506	6,500	0.0300	1	2008-01-23 11:20:40	0.00
0	REGS	SIF2	1,000	2.6800	2.6900	26,900	-	2.6900	-	2008-01-23 11:20:40	1.13
0	REGS	BRK	4,900	2.1100	2.1200	5,800	-	2.1200	-	2008-01-23 11:20:41	3.41
1	XMBS	FERO	-	-	-	-	200	0.5500	1	2008-01-23 11:20:42	-8.33
1	XMBS	FERO	5,638	0.5200	0.6900	13,400	62	0.5200	1	2008-01-23 11:20:42	-13.33
1	REGS	TLV	522,900	0.7250	0.7300	15,900	10,000	0.7300	1	2008-01-23 11:20:43	1.39
1	REGS	BIO	5,000	0.4480	0.4490	71,500	8,000	0.4480	2	2008-01-23 11:20:43	-0.22
1	REGS	TLV	522,900	0.7250	0.7300	12,900	3,000	0.7300	1	2008-01-23 11:20:44	1.39
1	REGS	TLV	522,900	0.7250	0.7300	12,700	200	0.7300	1	2008-01-23 11:20:44	1.39
1	REGS	SIF3	29,000	1.7500	1.7700	300	9,000	1.7500	1	2008-01-23 11:20:45	0.00
1	REGS	SIF4	9,200	1.8200	1.8300	9,600	500	1.8300	2	2008-01-23 11:20:45	0.00
1	REGS	TLV	682,500	0.7200	0.7250	77,100	522,900	0.7250	17	2008-01-23 11:20:46	0.69
0	REGS	TEL	240	32.2000	32.7000	40	-	32.2000	-	2008-01-23 11:20:47	0.00
1	REGS	EFO	-	-	-	-	1,000	1.5700	1	2008-01-23 11:20:48	-1.26
1	REGS	EFO	1,000	1.5300	1.5500	1,000	1,000	1.5500	1	2008-01-23 11:20:48	-2.52
1	RGBS	PRSN	2,420	21.2000	22.2000	50	470	21.5000	1	2008-01-23 11:20:49	-1.83
0	RGBS	PRSN	470	21.5000	22.2000	50	-	21.5000	-	2008-01-23 11:20:49	-1.83
0	RGBS	PRSN	470	21.5000	22.1000	50	-	21.5000	-	2008-01-23 11:20:50	-1.83
1	XMBS	AUCS	100	20.1000	20.5000	37	6	20.2000	1	2008-01-23 11:20:50	6.88
0	REGS	TLV	682,500	0.7200	0.7250	80,100	-	0.7250	-	2008-01-23 11:20:54	0.69
0	RGBS	ARDF	29,000	0.1120	0.1160	40,000	-	0.1120	-	2008-01-23 11:20:55	0.90
1	REGS	BIO	300	0.4450	0.4480	2,000	5,000	0.4480	2	2008-01-23 11:20:55	-0.22
1	REGS	BRD	760	20.3000	20.4000	2,270	500	20.3000	3	2008-01-23 11:20:56	1.50
1	REGS	BRD	5,010	20.2000	20.3000	240	760	20.3000	2	2008-01-23 11:20:57	1.50
1	REGS	SIF4	9,200	1.8200	1.8300	9,100	500	1.8300	1	2008-01-23 11:20:58	0.00

 

 

for each simbol i select the ones i need:

 

let's say simbol=SIF1

 

1	REGS	SIF1	1,400	2.7200	2.7300	4,000	2,700	2.7200	2	2008-01-23 11:20:25	0.74
0	REGS	SIF1	4,100	2.7200	2.7300	4,000	-	2.7400	-	2008-01-23 11:20:14	1.48

 

but i only take those that has 1 in the first column

 

1 REGS SIF1 1,400 2.7200 2.7300 4,000 2,700 2.7200 2 2008-01-23 11:20:25 0.74

 

 

here could be more lines

 

but from this one i get this:

 

 

<ticker>,<per>,<date>,<time>,                    <open>,    <high>,    <low>,      <close>,    <volume>,<openint>

SIF1,I,01/23/2008,11:20:00,                      2.7200,    2.7200,      2.7200,      2.7200,      2700,0

 

Link to comment
https://forums.phpfreaks.com/topic/94777-improving-code/#findComment-485386
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.