Data Structure Introduction

Data structure is the tool you wanna use in order to plan to any project. So we gonna share some threads about it from the begaining in a very simple way.

No alt text provided for this image
No alt text provided for this image

Let's assume that we wanna make a function that a number as a param and returns the sum of it's index starting from 1 .. in the default way we will make it as in the image "add1Func".



Maybe a fewer implement it as the second way "add2Func" and there is more. they all do the job which is returning the expected result so why shall we care ?!


Okay at this level we know that we are doing the job, so let's do it professionally bec in a big scale project every line matters & we wanna make it the right way.

The Q that Data Structure was created for is which way is better? in order to answer we shall define what's the elements that makes it better ?

  1. Faster?
  2. Less Memory-Intensive?
  3. More Readable?

In this article we'll focus on the first two :

  1. In order to have a good performance we must insure that our code has the fewer execution time. So we gonna measure the time that every way took to return the same result as in the images upove.

  • We gonna see clearly that there's a differences as the second way is waaay faster that the first although they doing the same job!! (note that both same & differenet machines will record different times as the time varies in a slight margin).

2. If not counting time is the only measurement tool then we can count the simple operations needed to preform the same result.

1
Second Way


  • As in image the second way took only 3 static operations regardless the size of "n".



No alt text provided for this image

  • While in pic 2 the other way took a variable number of operations and two static operations. So if we added 10 as a param then we'll have 50 operations plus 2 static, if we added 20 then it's 100 operations plus 2 static & so on ..


  • So what it matters that if it's Static or Dynamic operation as the static will preform more better & less time taken.

We gonna discuss how we can calculate that with O notation in the next article, hope you enjoyed it.

Best Wishes ??

Ahmed Medhat



要查看或添加评论,请登录

社区洞察

其他会员也浏览了