Pascal程序运行速度比c ++快......原因是什么? [英] Pascal program runs faster than c++...what's the reason ?

查看:191
本文介绍了Pascal程序运行速度比c ++快......原因是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我必须解决一个问题:

给定一个包含N个数字的文件我必须输出所有数字之前的最大数字小于它且之后的所有数字都大于它..如果没有这样的数字,输出'找不到'!

所以我在pascal上写了这个代码

So I had to solve a problem :
given a file that contains N numbers I have to output the largest number that all numbers before it are less than it and all numbers after it are larger than it..if there is no such number the output is 'not found' !
so I wrote this code on pascal

program solar  ;
uses crt,dos,sysutils;

var i,k,m,j,min,max,pos:longint;
a,mi,ma:array [1..1000000] of longint;
filein,fileout:textfile;
        hours:word;
        minutes:word;
        seconds:word;
        sec100:word;
procedure startclock;
        begin
        gettime(hours,minutes,seconds,sec100);
        end;
procedure stopclock;
        var seconds_count:longint;
        c_hours:word;
        c_minutes:word;
        c_seconds:word;
        c_sec100:word;
        mil:longint;
        begin
        gettime(c_hours,c_minutes,c_seconds,c_sec100);
        seconds_count:=(c_seconds-seconds)* 100 +(c_minutes-minutes) * 6000 + (c_hours-hours)*360000;
        mil:=seconds_count+c_sec100-sec100;
        writeln(mil/100:4:2,' seconds');
        end;

  
Begin  
  startclock;
  assign(filein,'solar.in');  
  reset(filein);  
  readln(filein,m);  
  
  i:=0;  
  repeat  
  i:=i+1;  
  readln(filein,a[i]);  
  until eof(filein);  
  close(filein);  
  
  ma[1]:=a[1];  
      mi[m]:=a[m];  
     for i:=2 to m do  
  begin  
      if a[i]>ma[i-1] then  
        begin  
                        ma[i]:=a[i];  
        end  
      else  
        begin  
           ma[i]:=ma[i-1];  
        end;  
  end;  
  pos:=-1;  
  i:=m-1;  
  while i>=2 do  
  begin  
   if a[i]<mi[i+1] then  
       begin  
  
          mi[i]:=a[i];  
       end  
       else  
        begin  
         mi[i]:=mi[i+1];  
        end;  
              if (a[i]>ma[i-1])  and  (a[i]<mi[i+1]) then  
                begin  
                pos:=a[i];  
                break;  
                end;  
  i:=i-1;  
  
  
  end;  
  
  
  
    assign(fileout,'solar.out');  
       rewrite(fileout);  
    if pos=-1 then  
    writeln(fileout,'NOT FOUND')  
    else  
    writeln(fileout,pos);  
    close(fileout);  
stopclock();
readkey();
    end.  



当我使用输入80.000 numberit运行需要20 ms



但是当我运行它时(c ++ ):




when I run with an input of 80.000 numbersit takes 20 ms

but when I run this (c++) :

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <ctime>


using namespace std;


int main()
{
	clock_t t1,t2;
	t1=clock();
	ifstream fin;ofstream fout;
	fin.open("solar.txt");
		int x;
		int arr[80000];int min[80000];int max[80000];
	
		int result(-1);

	fin>>x;
///	arr=new int[x-1];
//	min=new int[x-1];

//	max=new int[x-1];
	fin>>arr[0];
	max[0]=arr[0];
	for (int i=1;i<x;i++)
	{ int temp;fin>>temp;
	arr[i]=temp;
	if(temp>max[i-1]) max[i]=temp; else max[i]=max[i-1];

		}
	min[x-1]=arr[x-1];

		for (int i=x-2;i>=0;i--)
	{

		if(arr[i]<min[i+1]) min[i]=arr[i]; else min[i]=min[i+1];
	
		if(arr[i]<min[i+1] && arr[i]>max[i-1]) { result=arr[i];break;}
	}
		fout.open("solarout.txt");
		if(result==-1) fout<<"NOT FOUND"<<endl;
		else
			fout<<result<<endl;
		t2=clock();
		fin.close();fout.close();


		cout<<t2-t1<<endl;
		system("pause");


}



输入相同需要300毫秒

也...我可以在C ++上声明一个包含许多单元格的静态数组...如果我尝试像在pascal中那样声明一个1.000.000大小的数组,程序会崩溃...

我的问题是:

1)为什么会发生这种情况

2)为什么我不能声明大的静态数组?

3)是动态数组(不是链接列表向量)等等......只是使用NEW关键字声明的动态数组比静态数组慢(假设我执行相同的代码)

4)是否有更有效(有效=快速)的方法来解决这个问题我的b $ b我做的是:a)传递一个数组中的数字,b)得到一个单元格的最大数量和之前的单元格并保存另一个相同大小的数组,c)得到一个单元格的最小数量和它后面的单元格,并检查单元格中的数字是否符合标准...如果是,我打破循环,因为我想要最大的数字,这意味着它之前的所有其他数字比...更多...

非常感谢!


with the same input it takes 300 ms
also...I can't declare a static array with many cells on C++...if I try to declare a 1.000.000 size array like I did in pascal the program crashes...
my questions are :
1) why does this happen
2) why can't I declare big static arrays ?
3) are dynamic arrays (not linked lists vectors etc...just dynamic arrays declared using the NEW keyword ) slower than static ones (suppose I execute the same code)
4) Is there a more effective (effective=fast) way to solve this problem ?
what I do is : a) pass the numbers in an array, b) get the maximum number of a cell and the ones before it and save it an another same-sized array,c) get the minimum number of a cell and the ones after it,and check if the number in the cell meets the criteria...if it does I break the loop since i want the largest number and that means all other numbers before it are less than it...
thank you very much !

推荐答案

输入占用程序中最大的时间和输出功能。所以你所测量的是你的pascal实现的输入/输出系统显然比你的C实现的输入/输出系统快得多。这并不奇怪,因为流i / o从来都不是最快的。使用其他编译器和运行时系统可能会改变这种情况。您还没有告诉我们您正在使用哪种编译器,因此很难发表评论。



从大型静态数组开始,您尝试了什么?可能是你还在使用16位实现吗?



至于问题(3):对堆上数组的访问并不慢访问静态分配的数组或堆栈上分配的数组。只是他们的分配/取消分配可能需要花费一点时间。



不,我没有看到比两次线性扫描更快的方法来解决这个问题。 br />


如果要在两种编程语言之间进行有意义的比较,则应将时间测量的范围限制为纯算法,并排除所有i / o工作。
The largest amount of time in your program is taken up by the input and output functions. So what you are measuring is that the input/output system of your pascal implementation is obviously much faster than the input/output system of your C implementation. That is not really surprising, because stream i/o has never been the fastest. And using other compilers and runtime systems might change the situation. You have not told us which compiler you are working with, so it is hard to comment.

As of the big static arrays, what have your tried? Could it be that you are still working with a 16-bit implementation?

And as for question (3): The access to arrays on the heap is not slower that the access to statically allocated arrays, or arrays allocated on the stack. Just their allocation / deallocation may take take up a little time.

And no, I don't see a faster method to solve this than with two linear scans.

If you want to do meaningful comparison between the two programming languages, you should limit the scope of your time measurement to the pure algorithm and exclude all i/o work.


你问:



2)为什么我不能声明大静态数组?



1.你的两个程序不等同。

2. C ++代码中的数组定义不是静态的 - 所有三个都在堆栈中声明。由于几个原因,这对于这么大的东西来说是禁忌。如果你想要大的数组,用operator new *或*分配它们将'static'关键字放在所有三个定义之前*或*将数组定义移到main()之外。



在提出类似这样的问题时,请提供一些信息,例如您正在使用的操作系统以及您正在使用的编译器。您可以提供更多信息,但如果没有讲座,较少的信息将无法为您提供帮助;-)



总有更有效的方法实施*任何*解决方案,但你没有告诉我们足够的帮助你在那个领域。你知道什么是任务,我们不知道。
You asked:

2) why can't I declare big static arrays ?

1. Your two programs are not equivalent.
2. Your array definitions in the C++ code aren't static - all three are declared on the stack. That's a no-no for stuff this large for several reasons. If you want arrays that large, allocate them with operator new *or* put the 'static' keyword in front of all three definitions *or* move the array definitions outside of main().

When asking questions like this one, please provide information such as which operating system you're using and which compiler(s) you're using. You can provide more information, but less information won't get you the help you're looking for without a lecture ;-)

There are always more effective ways to implement *any* solution, but you haven't told us enough to help you in that area. You know what the task is and we don't.


这篇关于Pascal程序运行速度比c ++快......原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆