⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex5_8.cpp

📁 The source code for "C++ Solutions". This book is a companion to the book that is widely recognized
💻 CPP
字号:
/* Code for exercise 5.8.
 | 
 | "C++ Solutions--Companion to The C++ Programming Language, Third Edition"
 | by David Vandevoorde; Addison-Wesley 1998; ISBN 0-201-30965-3.
 |
 | Permission to use, copy, modify, distribute and sell this software
 | and its documentation for any purpose is hereby granted without fee.
 | The author makes no representations about the suitability of this
 | software for any purpose.  It is provided "as is" without express or
 | implied warranty.
 `----------------------------------------------------------------------*/

// Note: Some compilers do not like the repeated "for (int k ..."
// statement. A common trick to work around that anachronism, is to
// define the following macro:
// #define for if (0) ; else for

#include <iostream>
#include <time.h> // For clock()
#include <stddef.h> // For size_t
#include <string.h> // For memcpy()

void copy_with_pointers(char const *src, char *dst,
                        size_t n) {
   for (size_t k = 0; k!=n; ++k)
      *dst++ = *src++;
}

void copy_with_indices(char const *src, char *dst,
                       size_t n) {
   for (size_t k = 0; k!=n; ++k)
      dst[k] = src[k];
}

int main() {
   int const n_bytes = 100000;
   int const n_calls = 100;
   char *src = new char[n_bytes];
   char *dst = new char[n_bytes];

   // Reduce paging effects by accessing all bytes:
   memcpy(dst, src, n_bytes);
   clock_t start, end, reftime;

   // Test 1 (reference time)
   start = clock();
   for (int k = 0; k!=n_calls; ++k)
      memcpy(dst, src, n_bytes);
   end = clock();
   reftime = end-start;

   // Test 2 (pointers)
   start = clock();
   for (int k = 0; k!=n_calls; ++k)
      copy_with_pointers(src, dst, n_bytes);
   end = clock();
   cout << "With pointers: "
        << static_cast<double>(end-start)/reftime
        << " times slower than memcpy.\n";

   // Test 3 (indices)
   start = clock();
   for (int k = 0; k!=n_calls; ++k)
      copy_with_indices(src, dst, n_bytes);
   end = clock();
   cout << "With indices: "
        << static_cast<double>(end-start)/reftime
        << " times slower than memcpy.\n";

   delete[] src;
   delete[] dst;
   return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -