Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <windows.h>
- #include <ctime>
- #include <iostream>
- #include <stdio.h>
- #include <time.h>
- using namespace std;
- void ASMsort(int arr[],int length) {
- int l = length;
- _asm {
- start:
- mov esi, arr
- a2 : mov eax, l
- mov ecx, eax
- xor ebx, ebx
- a3 : mov eax, [esi + ecx * 4 - 4] // получаем значение очередного элемента
- cmp [esi + ecx * 4], eax // сравниваем со значением соседнего элемента
- jnb a1 // если больше или равен - идем к следующему элементу
- setna bl
- xchg eax, [esi + ecx * 4] // меняем значение элементов местами
- mov [esi + ecx * 4 - 4], eax
- a1 : loop a3 // двигаемся вверх до границы массива
- add esi, 4 // сдвигаем границу отсортированного массива
- dec ebx // проверяем были ли перестановки
- jnz a0 // если перестановок не было - заканчиваем сортировку
- dec l // уменьшаем количество неотсортированных элементов
- jnz a2 // если есть еще неотсортированные элементы - начинаем новый проход
- a0 :
- }
- }// сортировки на ассемблере
- void BUBBLEsort(int arr[], int n) {
- int i, j, tmp;
- n = n - 1;
- for (i = n; i > 0; i--) { // i - номер прохода
- for (j = 0; j < i; j++) {
- if (arr[j] > arr[j + 1]) {
- tmp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = tmp;
- }
- }
- }
- }
- int main() {
- cout << " Number of elements: 10000 \n";
- const int length = 10000;
- int i, arr_for_c[length], arr_for_asm[length];
- double t1, t2, t_c, t_asm; // время
- clock_t t3, t4,t7,t8;
- double t5, t6;
- time_t start, end;
- for (i = 0; i < length; i++) {
- arr_for_c[i] = arr_for_asm[i] = rand();
- } // заполнение случ числами
- t1 = (double)GetTickCount(); t3 = clock();
- BUBBLEsort(arr_for_c, length);
- t2 = (double)GetTickCount(); t4 = clock();
- t_c = t2 - t1;
- t5 = (double)(t4 - t3);
- t1 = (double)GetTickCount(); t3 = clock();
- ASMsort(arr_for_asm, length);
- t2 = (double)GetTickCount(); t4 = clock();
- t_asm = t2 - t1;
- t6 = (double)(t4 - t3);
- cout << " c sort: ";
- //for (i = 0; i < 10000; i++) { printf("%d ", arr_for_c[i]); }
- cout << "\n asm sort: ";
- //for (i = 0; i < 10000; i++) { printf("%d ", arr_for_asm[i]); }
- cout<<"\n\n";
- printf(" c time: %f seconds\n", t_c);
- printf(" asm time: %f seconds\n",t_asm);
- cout << "\n";
- printf(" c time: %f seconds\n", t5);
- printf(" asm time: %f seconds\n", t6);
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement