Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <windows.h>
  5. #include <ctime>
  6. #include <iostream>
  7. #include <stdio.h>
  8. #include <time.h>
  9.  
  10. using namespace std;
  11.  
  12. void ASMsort(int arr[],int length) {
  13.     int l = length;
  14.     _asm {
  15.     start:  
  16.            
  17.              mov esi, arr
  18.         a2 : mov eax, l
  19.              mov ecx, eax
  20.              xor ebx, ebx
  21.         a3 : mov eax, [esi + ecx * 4 - 4]  // получаем значение очередного элемента
  22.              cmp [esi + ecx * 4], eax       // сравниваем со значением соседнего элемента
  23.              jnb a1                        // если больше или равен - идем к следующему элементу
  24.              setna bl
  25.              xchg eax, [esi + ecx * 4]     // меняем значение элементов местами
  26.              mov [esi + ecx * 4 - 4], eax
  27.         a1 : loop a3                       // двигаемся вверх до границы массива
  28.              add esi, 4                    // сдвигаем границу отсортированного массива
  29.              dec ebx                       // проверяем были ли перестановки
  30.              jnz a0                     // если перестановок не было - заканчиваем сортировку
  31.              dec l                 // уменьшаем количество неотсортированных элементов
  32.              jnz a2                       // если есть еще неотсортированные элементы - начинаем новый проход
  33.         a0 :
  34.  
  35.     }
  36. }//  сортировки на ассемблере
  37.  
  38. void BUBBLEsort(int arr[], int n) {
  39.     int i, j, tmp;
  40.     n = n - 1;
  41.  
  42.     for (i = n; i > 0; i--) {        // i - номер прохода
  43.         for (j = 0; j < i; j++) {
  44.             if (arr[j] > arr[j + 1]) {
  45.                 tmp = arr[j];
  46.                 arr[j] = arr[j + 1];
  47.                 arr[j + 1] = tmp;
  48.             }
  49.         }
  50.     }
  51. }
  52.  
  53.  
  54. int main() {
  55.     cout << "   Number of elements: 10000 \n";
  56.     const int length = 10000;
  57.     int i, arr_for_c[length], arr_for_asm[length];
  58.     double t1, t2, t_c, t_asm; // время
  59.     clock_t t3, t4,t7,t8;
  60.     double t5, t6;
  61.     time_t start, end;
  62.     for (i = 0; i < length; i++) {
  63.         arr_for_c[i] = arr_for_asm[i] = rand();
  64.     } // заполнение случ числами
  65.  
  66.  
  67.  
  68.     t1 = (double)GetTickCount(); t3 = clock();
  69.     BUBBLEsort(arr_for_c, length);
  70.     t2 = (double)GetTickCount(); t4 = clock();
  71.     t_c = t2 - t1;
  72.     t5 = (double)(t4 - t3);
  73.  
  74.     t1 = (double)GetTickCount();    t3 = clock();
  75.     ASMsort(arr_for_asm, length);
  76.     t2 = (double)GetTickCount();    t4 = clock();
  77.     t_asm = t2 - t1;
  78.     t6 = (double)(t4 - t3);
  79.  
  80.  
  81.     cout << "   c sort:     ";
  82.     //for (i = 0; i < 10000; i++) { printf("%d ", arr_for_c[i]); }
  83.     cout << "\n   asm sort: ";
  84.     //for (i = 0; i < 10000; i++) { printf("%d ", arr_for_asm[i]); }
  85.     cout<<"\n\n";
  86.  
  87.     printf("   c time: %f seconds\n", t_c);
  88.     printf("   asm time: %f seconds\n",t_asm);
  89.     cout << "\n";
  90.     printf("   c time: %f seconds\n", t5);
  91.     printf("   asm time: %f seconds\n", t6);
  92.  
  93.    
  94.    
  95.     _getch();
  96.  
  97.  
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement