tawhidmonowar's blog

By tawhidmonowar, history, 13 months ago, In English

In programming, “++i” and “i++” are both used to increment the value of a variable by 1, but the difference is in the order in which the increment operation and the use of the variable occur.

When it comes to the difference between “++i” and “i++”, it’s important to understand how each operator works. “++i” is known as the pre-increment operator, which increments the value of ‘i’ immediately and returns the incremented value. On the other hand, “i++” is known as the post-increment operator, which increments the value of ‘i’ but returns the original value that ‘i’ held before being incremented.

Example:

Code

In terms of performance, “++i” is sometimes faster than “i++” and is never slower than "i++". For intrinsic types like int, it doesn’t matter: “++i” and “i++” are the same speed. However, for class types like iterators, “++i” very well might be faster than “i++” since the latter might make a copy of the this object.

In conclusion, while there may be some performance differences between “++i” and “i++”, it’s generally recommended to use “++i” unless you specifically want the postfix semantics.

I hope this information will be helpful to you.

Full text and comments »

  • Vote: I like it
  • +36
  • Vote: I do not like it

By tawhidmonowar, history, 15 months ago, In English

Hello programmers! I've put together a document on Bit Manipulation in C++ that I think you'll find useful. It covers essential bitwise operators and techniques that can help you optimize your code and tackle complex programming problems.

Check it out and let me know what you think!

View
Download

Full text and comments »

  • Vote: I like it
  • -8
  • Vote: I do not like it

By tawhidmonowar, history, 16 months ago, In English

Experimental time complexity analysis is a way to estimate the time complexity of an algorithm by running it on various inputs and measuring the running time. Here is an example of how to perform experimental time complexity analysis in C++:

#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono;

void someAlgorithm(int n) {
  // code for the algorithm
}

int main() {
  int n;
  cin >> n;

  auto start = high_resolution_clock::now();
  someAlgorithm(n);
  auto stop = high_resolution_clock::now();
  auto duration = duration_cast<microseconds>(stop &mdash; start);

  cout << "Time taken by function: "
       << duration.count() << " microseconds" << endl;
  return 0;
}

In this example, we have a function someAlgorithm() that takes an input parameter n. We use the library to measure the running time of the function by recording the start time and end time using high_resolution_clock::now(), and then calculating the duration using duration_cast(stop — start).

To perform the experimental time complexity analysis, we can run the someAlgorithm() function with various input sizes (for example, 10, 100, 1000, 10000, etc.), and record the corresponding running times. We can then plot the running times against the input sizes on a graph, and try to fit a curve to the data. The shape of the curve can give us an idea of the time complexity of the algorithm. For example, if the curve looks linear, then the algorithm is likely to have linear time complexity. If the curve looks quadratic, then the algorithm is likely to have quadratic time complexity.

However, it is important to note that experimental time complexity analysis is not a rigorous method and should be used with caution.

Full text and comments »

  • Vote: I like it
  • +16
  • Vote: I do not like it