return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - Space Complexity js functions - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Space Complexity js functions - Stack Overflow

programmeradmin0浏览0评论

What is the space plexity and how we can obtain it? (where a and b are arrays of numbers of size N and M)

a.filter(function(v) {
   return !b.includes(v);
})

What is the space plexity and how we can obtain it? (where a and b are arrays of numbers of size N and M)

a.filter(function(v) {
   return !b.includes(v);
})
Share Improve this question edited May 9, 2018 at 17:54 RaphaMex 2,8391 gold badge15 silver badges31 bronze badges asked May 9, 2018 at 17:19 dream123dream123 1294 silver badges14 bronze badges 1
  • 1 Space plexity is the proportion of additional memory your algorithm needs related to the size of the inputs (N and M). What additional memory do filter and includes need? – RaphaMex Commented May 9, 2018 at 17:27
Add a ment  | 

1 Answer 1

Reset to default 9

Space plexity is a mathematical measure of amount of memory your algorithm/function/program needs to store it's variables. Just like time plexity is measure of how much time your function needs to run.

TL;DR You cannot obtain it by any JS function. It is something you have to "calculate". In your algorithm, using two arrays, the space plexity would be O(n).

Explanation If you want to know bit more about space plexity: In puter science world the space plexity uses the "big Oh" notation (O(something)). And it is something you find out by analyzing your function. Usually by taking a "long hard stare" at your code

For example this function:

function add(x,y) { return x+y; }

Has space plexity of O(1). Because it uses two variables to store its data: x and y. So technically you use two "places" in your memory space. The question is, since you use two places, why the plexity is O(1)? Answer to that is, that plexity is expressed in "scale". That means, that if your function needs 1, 2, 3, ... 5 variables to run we are still talking about "ones" or constants. Therefore O(1) or a "constant plexity".

Another example :

function sum(arr, n) {
  var sum = 0;
  for(var i=0; i<n; i++) {
    sum += arr[i];
  }
}

In this case the space plexity of this function is O(N) Why? Because we are using an array of certain length. This array can have length of 3, but it can also store 100 000 values. Since we are not talking about just "ones" here (or, we cannot be sure that it would be just few values), puter science guys decided that we would denote it as O(N) or "linear plexity".

And so on and on. For example, if you would need a 2D array in your program it would probably have O(N^2) or "quadratic plexity". In some cases you can bump into programs with logarithmic and (hopefully not) "exponential" space plexities of O(log N) or 2^O(N).

Read more about it here, here and here (it's about time lexity, but the measure is the same)

发布评论

评论列表(0)

  1. 暂无评论