Hot answers tagged

13

Maybe. The compiler might decide "hey, this function is only called a few times, and I'm supposed to optimize for speed, so I'll just inline this function". Essentially, the compiler will replace the function call with the body of the function. For example, the source code would look like this. void DoSomething() { a = a + 1; DoSomethingElse(a); } ...


6

There are multiple implementations of malloc and they can use very different algorithms. Two very widely used implementations are jemalloc and dlmalloc. In both cases the links have a lot of information about the thought process and trade-offs made in a general purpose allocator. Bear in mind a malloc implementation has very little information to go on, ...


4

Using exceptions for flow control is highly frowned upon in most languages, but not in Python. Using exceptions for flow control in Python is "pythonic." Exceptions are at the heart of how python for loops work, which terminate the iteration on receiving a StopIteration exception. Python has a slightly derogatory term for languages such as C++, Java, and C# ...


4

Comments turned into an answer: You are right to worry about performance with locking everything under one mutex, but the better solution is to make sure there is as little going on as possible inside the lock. Thread 1 should have the value and index ready and really only be doing a single write. Thread 2 would operate on an unshared local instance of the ...


3

If you care only about efficiency, here is a standard conforming and very efficient implementation: void* malloc(size_t sz) { errno = ENOMEM; return NULL; } void free(void*p) { if (p != NULL) abort(); } I'm pretty sure you won't find any faster malloc. But while still conforming to the standard, that implementation is useless (it never ...


3

First, malloc and free work together, so testing malloc by itself is misleading. Second, no matter how good they are, they can easily be the dominant cost in any application, and the best solution to that is to call them less. Calling them less is almost always the winning way to fix programs that are malloc-limited. One common way to do this is to recycle ...


3

This is a matter of implementation of the compiler or runtime (and its options) and cannot be said with any certainty. Within C and C++, some compilers will inline calls based on optimization settings - this can be trivially seen by examining the generated assembly when looking at tools such as https://gcc.godbolt.org/ Other languages, such as Java have ...


2

You're looking for performance in the wrong place. The problem with function calls is not that they cost much. There is another problem. Function calls could be absolutely free, and you would still have this other problem. It is that a function is like a credit card. Since you can easily use it, you tend to use it more than maybe you should. Suppose you ...


2

Python has a builtin method setdefault which does exactly what you need: self.layers.setdefault("pos", []).extend(node)


2

I think that the two SUT are not direct comparisons. I would not be surprised at any comparable difference when you consider all the variables: memory manufacture, motherboard architecture, compiler version (that compiled malloc), what the memory space application is like on the SUT at the time, etc etc etc ....... Try using your test harness to be more ...


2

After thinking twice about your solution, I am sure that your algorithm works well. IMHO it could be seen as a variant of lazy evaluation. Here is another variant. Change smooth_once so it only puts the pairs (index,x) into a queue (lets call the arrays indexes and xs). function smooth_once(index, x): indexes.append(index) xs.append(x) Here is the ...


2

In your particular case (all variables being scalars, i.e. integral or boolean) you might consider using the atomic facilities of C++11. You need a recent GCC or Clang compiler. So you would use std::atomic_bool and e.g. std::atomic_int etc... for the types of these variables and use atomic_load & atomic_store. A simple usage would be to systematically ...


1

The main problem with your malloc_quick() implemenation is, that it is not thread-safe. And yes, if you omit thread-support from your allocator, you can achieve a significant performance gain. I have seen a similar speedup with my own non-thread-safe allocator. However, a standard implementation needs to be thread-safe. It needs to account for all of the ...


1

If you compare a real malloc implementation with a school project, consider that a real malloc has to manage allocations, reallocations and freeing memory of hugely different sizes, working correctly if allocations happen on different threads simultaneously, and reallocation and freeing memory happen on different threads. You also want to be sure that any ...


1

I measured the overhead of direct and virtual C++ function calls on the Xenon PowerPC some time ago. The functions in question had a single parameter and a single return, so parameter passing occurred on registers. Long story short, the overhead of a direct (non-virtual) function call was approximately 5.5 nanoseconds, or 18 clock cycles, compared to an ...


1

That depends on how long time it takes to perform the overhead with the function call (setting up stack frame, passing variables, clearing up afterwards...) compared to time spent inside the function doing useful work. (Language, compiler and compiler setting dependent) Anyway, in order to actually notice this a significant time of the execution must be ...


1

I think it really depends on the language and on the function. While the c and c++ compilers can inline a lot of functions, this is not the case for Python or Java. While I do not know the specific details for java (except that every method is virtual but I suggest you to check better the documentation), in Python I am sure that there is no inlining, no ...


1

The most efficient would probably be a binary format which you could read directly into memory, skipping the parsing step.


1

It is hard to say from the level of detail you provide, what exactly is the case. When you are saying that has recently been converted from .NET2 to .NET4. this could mean different things: ranging from »The compiler was just a .NET4 compiler and compiled for that target« to »Hey, there are such niceties in terms of Parallel Programming. Let's rewrite ...



Only top voted, non community-wiki answers of a minimum length are eligible