رفتن به مطلب
مرجع رسمی سی‌پلاس‌پلاس ایران

قاسم رمضانی منش

مدیران مرجع
  • تعداد ارسال ها

    97
  • تاریخ عضویت

  • روز های برد

    25

تمامی مطالب نوشته شده توسط قاسم رمضانی منش

  1. بعد نظرتون درباره Comment ها چیه ؟ شما به چه شکل ترجیه میدید ؟ ۱- حالت اول : void sortArray(std::array<int, g_arrayElements> &array) { // Step through each element of the array // (except the last one, which will already be sorted by the time we get there) for (int startIndex = 0; startIndex < g_arrayElements - 1; ++startIndex) { // smallestIndex is the index of the smallest element we’ve encountered this iteration // Start by assuming the smallest element is the first element of this iteration int smallestIndex = startIndex; // Then look for a smaller element in the rest of the array for (int currentIndex = startIndex + 1; currentIndex < g_arrayElements; ++currentIndex) { // If we've found an element that is smaller than our previously found smallest if (array[currentIndex] < array[smallestIndex]) // then keep track of it smallestIndex = currentIndex; } // smallestIndex is now the smallest element in the remaining array // swap our start element with our smallest element (this sorts it into the correct place) std::swap(array[startIndex], array[smallestIndex]); } } ۲- حالت دوم : void sortArray(std::array<int, g_arrayElements> &array){ // Step through each element of the array // (except the last one, which will already be sorted by the time we get there) // smallestIndex is the index of the smallest element we’ve encountered this iteration // Start by assuming the smallest element is the first element of this iteration // Then look for a smaller element in the rest of the array // If we've found an element that is smaller than our previously found smallest // smallestIndex is now the smallest element in the remaining array // swap our start element with our smallest element (this sorts it into the correct place) for (int startIndex = 0; startIndex < g_arrayElements - 1; ++startIndex){ int smallestIndex = startIndex; for (int currentIndex = startIndex + 1; currentIndex < g_arrayElements; ++currentIndex) { if (array[currentIndex] < array[smallestIndex]) smallestIndex = currentIndex; } std::swap(array[startIndex], array[smallestIndex]); } }
  2. اونا مثال بود. گفتم که مثلا شما وقتی با یک (تابع|متغیر) بر خورد میکنید. انتظار دارید که شکل ظاهریش به چه صورت باشه.. و همینطور نامگذاری متغیر ها. اسم (متغیر|تابع) طولانی و با معنی باشه راحت ترید ؟ یا اینکه اسم متغیر کوتاه و مختصر باشه ؟ مرسی بابت لینک . آها... اوکی مرسی
  3. با سلام ! به نظر برنامه نویسان حرفه ای ، وقتی یه پروژه ای (چند صد هزارخطی) به شما داده میشه چه انتظاری از نامگذاری توابع و متغیر ها دارید ؟ (چه انتظارات ظاهری از کد دارید ؟) برای مثال : کدام یکی از توابع زیر درک و خواندنش برای یک فرد خارجی (کسی که قصد ویرایش کد نوشته شد فرد دیگری را دارد) راحت تر است ؟ unsigned short int PrintArrayElemets(const int *Array); int printArray(const int *arr); سورس کد لینوکس (نوشته شده به زبان سی) تقریبا بیشتر قسمت ها توابع به اینصورت تعریف شده است ... آیا شما به عنوان یک ویرایشگر (برنامه نویسی حرفه ای) این روش را راحت برای خواندن و درک کد قبول میکنید ؟ unsigned short int PrintArrayElemets(const int *Array); int printArray(const int *arr);
  4. قاسم رمضانی منش

    درمورد کتابخونه ای که گفتی دارم الان یه چیز هایی میخونم :X واقعا عالیه ! همین ابزار در کنار قدرت بهینه سازی که خودمون میتونیم توی کد اعمال کنیم مثل مواردی که آقای شیری و اسدزاده گفتن سرعت فوق العاده ای رومیتونیم داشته باشیم . مرسی آقا بهنام راضی راضی ام متشکر
  5. قاسم رمضانی منش

    بله ! صدالبته که عملکرد انواع الگوریتم ها و روششون میتونه توی سرعت تاثیر داشته باشه. مثل اینکه درست منظور خودم را نگفتم:) هردو کدی که ارسال کردم به یک روش نوشته شده اند ولی یکی به زبان سی پلاس پلاس و با استفاده از کتابخانه های استاندارد و یکی با استفاده از زبان سی و کتابخونه های استاندارد خودش. البته این مورد بدیهی هست که سرعت کمی از سی به سی پلاس پلاس کاهش پیدا کنه. که بنده دنبال راهی بودم برای رفع این مشکل که آقا بهنام با معرفی فلگ بهینه سازی O- تا حدود بسیار زیادی این مشکل رو برطرف کردن.
  6. قاسم رمضانی منش

    معمولا خیلی بهتره که اینطور پردازش های سنگین را به صورت موازی انجام بدید مثلا از تکنیک Divide and Concur استفاده کنید. ولی با تغییراتی که دادم برای 20000 تا تقریبا 3 ثانیه طول کشید که سورت کنه! مرسی ممنون حتما درباره این تکنیک جستجو میکنم. با تغییراتی که دادید سرعت را از ۷ دقیقه به ۱ دقیقه کاهش دادید !. (شما با مقدار ۲۰۰۰۰ هزار اجرا کرده بودید. مقدار پیش فرض ۱۰۰۰۰۰ بود.)
  7. قاسم رمضانی منش

    مرسی متشکرم. بله سرعت رو به شدت بالابرد ! اما ! این فلگ هایی که گفتید فقط فلگ اول O3- برای بهینه سازی هست که باعث افزایش سرعت میشه. و فلگ دوم ffast-math- به کامپایلر اجازه میده که یک سری از قوانین IEEE و ISO و نقض کنه : GCC Command Options و گفته شده که در صورت استفاده از فلگ ffast-math- از هیچ کدام سطح های بهینه سازی O- استفاده نکنید که باعث خروجی اشتباه میشه !
  8. با سلام ! دو قطعه برنامه ی زیر یکی آرایه ۱۰۰۰۰۰ تایی در حافظه Stack ساخته و به صورت Random مقداردهی شده و Sort میشود. کد اول به زبان ++C و با استاندارد 11 نوشته شده است که حدود ۷ دقیقه و کد دوم به زبان C نوشته شده است که حدود 1 دقیقه زمان میبرد ! CPU : Intel i7 M 620 (4) @ 2.667GHz آیا راهی برای بهینه سازی سرعت اجرای برنامه ی نوشته شده به زبان ++C هست ؟ کد نوشته به زبان ++C : #include <iostream> #include <functional> #include <utility> #include <array> #include <random> #include <chrono> const unsigned int MAX_LENGTH = 100000; bool Compare(unsigned int FirstVariable,unsigned int SecondVariable){ if(FirstVariable < SecondVariable) return true; return false; } void SortArray(std::array<unsigned int,MAX_LENGTH> &MyArray,std::function<bool(unsigned int,unsigned int)> function){ for(unsigned int index=0;index < MAX_LENGTH;++index) for(unsigned int AnotherIndex=0;AnotherIndex<MAX_LENGTH;++AnotherIndex) if(function(MyArray[index],MyArray[AnotherIndex])) std::swap(MyArray[index],MyArray[AnotherIndex]); } void PrintArrayElements(const std::array<unsigned int,MAX_LENGTH> &MyArray){ for(const auto &item : MyArray) std::cout << item << std::endl; } void RandomizeArray(std::array<unsigned int,MAX_LENGTH> &MyArray){ std::mt19937_64 Random(static_cast<int>(std::chrono::high_resolution_clock::now().time_since_epoch().count())); std::uniform_int_distribution<> RandomGenerator(0,1000); for(unsigned int index=0;index<MAX_LENGTH;++index) MyArray[index] = static_cast<unsigned int>(RandomGenerator(Random)); } int main(){ std::array<unsigned int,MAX_LENGTH> MyOrginalArray; RandomizeArray(MyOrginalArray); SortArray(MyOrginalArray,Compare); PrintArrayElements(MyOrginalArray); return 0x0000; } کد نوشته شده به زبان C : #include <stdio.h> #include <stdlib.h> #include <time.h> #include <stdbool.h> #define MAX_LENGTH 100000 bool Compare(unsigned int FirstVariable,unsigned int SecondVariable){ if(FirstVariable < SecondVariable) return true; return false; } void SortArray(unsigned int *MyArray,bool (*compare)(unsigned int,unsigned int)){ unsigned int tmp=0; for(unsigned int index=0;index < MAX_LENGTH;++index){ for(unsigned int AnotherIndex=0;AnotherIndex<MAX_LENGTH;++AnotherIndex){ if(compare(MyArray[index],MyArray[AnotherIndex])){ tmp = MyArray[index]; MyArray[index] = MyArray[AnotherIndex]; MyArray[AnotherIndex] = tmp; } } } } void RandomizeArray(unsigned int *MyArray){ srand(time(NULL)); for(unsigned int index=0;index < MAX_LENGTH ;++index) MyArray[index] = rand() % 1000; } void PrintArrayElements(unsigned int *MyArray){ for(unsigned int index=0;index<MAX_LENGTH;++index) fprintf(stdout,"%d\n",MyArray[index]); } int main(){ unsigned int Array[MAX_LENGTH]; RandomizeArray(Array); SortArray(Array,Compare); PrintArrayElements(Array); return 0x0000; }
  9. قاسم رمضانی منش

    مرسی تشکر با نصب دو بسته آخر درست شد. سوال : از کجا متوجه شدید که این بسته ها مورد نیاز هست ؟
  10. قاسم رمضانی منش

    با سلام ؛ بنده به دلایلی مجبور به نصب دوباره سیستم عامل شدم و بعد از نصب Qt Creator در هنگام کامپایل یک پروژه (پیشفرض) به این مشکل بر خوردم و اجازه کامپایل را نمیدهد درصورتی که قبلا این چنین مشکلی نبوده ! g++ -Wl,-rpath,/home/ghasem/Software/Qt5.11.1/5.11.1/gcc_64/lib -o untitled1 main.o mainwindow.o moc_mainwindow.o -L/home/ghasem/Software/Qt5.11.1/5.11.1/gcc_64/lib -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread /usr/bin/ld: cannot find -lGL Makefile:257: recipe for target 'untitled1' failed collect2: error: ld returned 1 exit status make: *** [untitled1] Error 1 04:02:17: The process "/usr/bin/make" exited with code 2. Error while building/deploying project untitled1 (kit: Desktop Qt 5.11.1 GCC 64bit) The kit Desktop Qt 5.11.1 GCC 64bit has configuration issues which might be the root cause for this problem. When executing step "Make" # System Info - OS : `Debian GNU/Linux 9.5 (stretch) x86_64` - kernel : `4.9.0-7-amd64` - gnu g++ : `g++ (Debian 6.3.0-18+deb9u1) 6.3.0 20170516` - make : `GNU Make 4.1 Built for x86_64-pc-linux-gnu` - cmake : `cmake version 3.7.2` و در صورتی که این فلگ lGL- را از خط 41 فایل Makefile حذف کنم. برنامه بدون مشکل کامپایل و اجرا میشود ! :
×
×
  • جدید...