public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* memcpy question
@ 2008-10-07 17:51 Michael.Schmidt
  2008-10-07 18:01 ` Michael.Schmidt
  2008-10-07 18:07 ` John Love-Jensen
  0 siblings, 2 replies; 3+ messages in thread
From: Michael.Schmidt @ 2008-10-07 17:51 UTC (permalink / raw)
  To: gcc-help

Hello,

I apologize in advance if this is the wrong message board.  I've noticed
that memcpy seems to take a while (sometimes) for large chunks of memory
(about 1 MB).  

Here's a small sample application:

#include<iostream>
#include<time.h>
#include<cmath>
#include<iomanip>
#include <sys/timeb.h>

using namespace std;

double getTime()
{
  struct timeb tb ;

  ftime ( &tb );
  return (double) tb.time + (double) tb.millitm / 1000.0 ;
}

int main()
{
  char src[1048576] = {0};
  char dst[1048576];
  
  for(unsigned int i = 0; i < 25; ++i)
  {
    double start_time = getTime();
    memcpy(dst, src, 1048576);
    double end_time = getTime();
    cout << "Time: " << 1000.0*(end_time - start_time) << endl;
  }

  return 0;
}

Here's the output:

Time: 0.999928
Time: 0
Time: 0
Time: 0
Time: 0
Time: 0.999928
Time: 0
Time: 0
Time: 0
Time: 0
Time: 0
Time: 0
Time: 0
Time: 0.999928
Time: 0
Time: 0
Time: 0
Time: 0
Time: 0
Time: 0
Time: 1.00017
Time: 0
Time: 0
Time: 0
Time: 0

I would be eternally grateful if anyone could explain what's happening.
Run on a Core 2 Duo E6550, 4 GB RAM.

Thanks,
Mike

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: memcpy question
  2008-10-07 17:51 memcpy question Michael.Schmidt
@ 2008-10-07 18:01 ` Michael.Schmidt
  2008-10-07 18:07 ` John Love-Jensen
  1 sibling, 0 replies; 3+ messages in thread
From: Michael.Schmidt @ 2008-10-07 18:01 UTC (permalink / raw)
  To: gcc-help

Hello again,

My time function was imprecise.

double getTime()
{
  timeval tv ;

  gettimeofday ( & tv, NULL ) ;

  return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0 ;
}

Using the function above gives times of around 0.13 ms, which is much
better than what I've seen in my project.  I saw times that were more
than 2 ms.  What could slow memcpy down?

Thanks,
Mike


On Tue, 2008-10-07 at 12:50 -0500, Michael.Schmidt@L-3com.com wrote:
> Hello,
> 
> I apologize in advance if this is the wrong message board.  I've noticed
> that memcpy seems to take a while (sometimes) for large chunks of memory
> (about 1 MB).  
> 
> Here's a small sample application:
> 
> #include<iostream>
> #include<time.h>
> #include<cmath>
> #include<iomanip>
> #include <sys/timeb.h>
> 
> using namespace std;
> 
> double getTime()
> {
>   struct timeb tb ;
> 
>   ftime ( &tb );
>   return (double) tb.time + (double) tb.millitm / 1000.0 ;
> }
> 
> int main()
> {
>   char src[1048576] = {0};
>   char dst[1048576];
>   
>   for(unsigned int i = 0; i < 25; ++i)
>   {
>     double start_time = getTime();
>     memcpy(dst, src, 1048576);
>     double end_time = getTime();
>     cout << "Time: " << 1000.0*(end_time - start_time) << endl;
>   }
> 
>   return 0;
> }
> 
> Here's the output:
> 
> Time: 0.999928
> Time: 0
> Time: 0
> Time: 0
> Time: 0
> Time: 0.999928
> Time: 0
> Time: 0
> Time: 0
> Time: 0
> Time: 0
> Time: 0
> Time: 0
> Time: 0.999928
> Time: 0
> Time: 0
> Time: 0
> Time: 0
> Time: 0
> Time: 0
> Time: 1.00017
> Time: 0
> Time: 0
> Time: 0
> Time: 0
> 
> I would be eternally grateful if anyone could explain what's happening.
> Run on a Core 2 Duo E6550, 4 GB RAM.
> 
> Thanks,
> Mike
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: memcpy question
  2008-10-07 17:51 memcpy question Michael.Schmidt
  2008-10-07 18:01 ` Michael.Schmidt
@ 2008-10-07 18:07 ` John Love-Jensen
  1 sibling, 0 replies; 3+ messages in thread
From: John Love-Jensen @ 2008-10-07 18:07 UTC (permalink / raw)
  To: Michael.Schmidt, GCC-help

Hi Michael,

It appears that on your system, your routine is often running faster than
the granularity of your time source.

One way to compensate is by wrapping the memcpy in a for(int j = 0; j < 100;
++j) so that the routine does not run too fast for the granularity.

--Eljay

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-10-07 18:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-10-07 17:51 memcpy question Michael.Schmidt
2008-10-07 18:01 ` Michael.Schmidt
2008-10-07 18:07 ` John Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).