Thread: cout == slow (prime numbers)

  1. #16
    Registered User
    Join Date
    May 2003
    Posts
    32
    To heat511,

    Do you mind to share your source code with the public so that a newbee like me could learn something?


    Thanks.
    [SIZE= 4]My favorite search engine is http://www.ultimasurf.com [/size]

  2. #17
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    I wrote a quick 'n' dirty prime finder for fun. There are probably lots of optimisations I've missed, but it can find 10,000 primes starting from zero in 30 seconds.

    That is on my crappy PIII 800.

    Code:
    void FindPrimes(unsigned long HowMany, unsigned long StartNum)
    {
    	unsigned long *NumberArray;
    	LARGE_INTEGER PerfFreq, CurrentCount, ElapsedCount, LaterCount;
    	int ArrayCounter, Counter;
    	int Minutes, Seconds;
    	int DivBy;
    	int DivNum;
    	bool Timed = false;
    
    	NumberArray = new unsigned long[50000];
    
    	if(!StartNum)
    		StartNum = 1;
    
    	if(!QueryPerformanceFrequency(&PerfFreq))
    	{
    		cout << "No Performance counter installed, operation will not be timed.";
    	}
    	else
    	{
    		Timed = true;
    		QueryPerformanceCounter(&CurrentCount);
    	}
    
    	cout << "Working..." << endl;
    
    	ArrayCounter=0;
    
    	for(Counter=0 ; Counter<HowMany ; Counter++)
    	{
    		if(ArrayCounter == 50000)
    		{
    			SaveArray(NumberArray, ArrayCounter);
    			ArrayCounter=0;
    		}
    		
    		if(!(StartNum%2))
    		{
    			StartNum++;
    			Counter--;
    			continue;
    		}
    
    		DivBy = 0;
    
    		for(DivNum=1 ; DivNum!=StartNum ; DivNum++)
    			if(!(StartNum % DivNum))
    			{
    				DivBy++;
    				if(DivBy > 2)
    				{
    					StartNum++;
    					Counter--;
    					break;
    				}
    			}
    
    		if(DivBy <= 2)
    		{
    			NumberArray[ArrayCounter] = StartNum;
    			StartNum++;
    			ArrayCounter++;
    		}
    	}
    
    	SaveArray(NumberArray, ArrayCounter);
    
    	if(Timed)
    	{
    		QueryPerformanceCounter(&LaterCount);
    		ElapsedCount.QuadPart = LaterCount.QuadPart - CurrentCount.QuadPart;
    		Seconds = ElapsedCount.QuadPart / PerfFreq.QuadPart; 
    
    		if(Seconds > 60)
    		{
    			Minutes = Seconds/60;
    			for(int x=0 ; x<Minutes ; x++)
    				Seconds -= 60;
    		}
    		else
    			Minutes = 0;
    	}
    
    	cout << "Found " << Counter << " Prime Numbers in "
    		 << Minutes << " minutes and " << Seconds << " Seconds." << endl;
    	system("pause");
    
    	delete[] NumberArray;
    }
    Again it was quick and dirty so probably a little rough around the edges.
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-lvalue error
    By zlb12 in forum C Programming
    Replies: 1
    Last Post: 04-17-2009, 10:43 AM
  2. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. Massive Function Problem
    By Marc Sharp in forum C Programming
    Replies: 10
    Last Post: 11-19-2003, 08:49 PM
  5. Replies: 22
    Last Post: 11-08-2001, 11:01 PM