Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Im kind of new to lua (well, I actually learned basic lua and how to use lua from c), and I am working on a simple tetris game using lua in c. (it sounds weird, but just for practice :D)
And I have a few questions:
I was wondering if calling c function from lua is faster.
if Q1 is yes, is it ok to call drawing functions in c from lua?
Doesn't it cause any slow down in my program?
What is the recommended Lua_C programming (just my way to call the program that uses lua as a script language) structure?
Ex) what kind of code should be written in Lua? What kind of code should be written in C? (drawing code, logic code, and etc.)
Simple answer: No. It won't be faster. There is going to be some overhead related to the function call from Lua to C.
Calling the drawing function is okay, since the actual drawing will take a lot more time than the function call anyways.
The rule of thumb is usually to write the logic using a scripting language (in this case, Lua) and let C handle any performance intensive parts, such as rendering and heavy calculations. For example, you probably won't gain any performance by letting C handle the character movement and stuff like that. You could use a profiler to figure out which parts that might gain performance from a C implementation.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
How is functional programming useful over normal procedural languages like c or object oriented programming languages like c++ and where does it shine?
C lacks several features of functional programming that need to be worked around (likewise, while you can write in an object-oriented style in C, you need to work around several missing features as well).
C functions are not first-class objects. You cannot return a function from a function, store a function in a variable, or pass a function to another function. You cannot nest functions, and you cannot create anonymous functions. The workaround is that C does allow you to use pointers to functions, so you can write a function that takes a pointer to a function as an argument, but this is not as clean as what you can do in a language oriented towards functional programming.
C lacks closures, which are a way of capturing the “environment” of execution at a particular point in a program (namely, what variable names are bound to).
C lacks generics, except in the most broad sense. In most functional languages, it is possible to write one function which applies to a large number of different types because they don’t depend on specific attributes of those types.
C is a low level language giving the programmer the full control over the program execution. It was newer designed to thigh level abstract language.
Functional programming is not popular and most languages used nowadays are object oriented.
If you need the language which gives you ability to control the program and the environment you should consider C++
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm writing a language that compiles to C right now, and when I say IL I mean as in C is the language I write the code as to then generate assembly by another c compiler, e.g. gcc or clang.
The C code I generate, will it be more beneficial to:
If I do some simple opt passes (constant propagation, dead code removal, ...) will this reduce the amount of work the C compiler has to do, or make it harder because it's not really human C code?
If I were to compile to say three-address code or SSA or some other form and then feed this into a C program with functions, labels, and variables - would that make it easier or harder for the C compiler to optimize?
Which kind of link together to form the following question...
What is the most optimal way to produce good C code from a language that compiles to C?
Is it worth doing any optimisations at all and leaving that to the compiler?
Generally there's not much point doing peephole type optimisations because the C compiler will simply do those for you. What is expensive is a) wasted or unnecessary "gift-wrapping" operations, b) memory accesses, c) branch mispredictions.
For a), make sure you're not passing data about too much, because whilst C will do constant propagation, there's a limit to how far it can detect that two buffers are in fact aliases of the same underlying data. For b) try to keep functions short and operations on the same data together, also limit heap memory use to improve cache performance. For c), the compiler understand for loops, it doesn't understand goto loops. So it will figure that
for(i=0;i<N;i++)
will usually take the loop body, it wont figure that
if(++i < N) goto do_loop_again
will usually take the jump.
So really the rule is to make your automatic code as human-like as possible. Though if it's too human-like, that raises the question of what your language has to offer that C doesn't - the whole point of a non-C language is to create a spaghetti of gotos in the C source, a nice structure in the input script.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
While implementing a communication protocol, we have an encoder that traverses some structs recursively and encodes them into a binary message.
So far so good, but now the buffer has to split out into multiple chunks of fixed size, e.g. the upper size of receiving buffer. Since allocating memory for the full message and cutting it consequently seems to be too wasteful (the size of the message is --in theory-- not bounded), the idea is now to implement a coroutine with means of setjmp/longjmp.
At the moment, I have a prototype with two jump buffers - one buffer for resuming the encode function and the second one for simulating the return behavior of the function to jump back to its caller.
Well, it seems to work, but the code looks like coming straight from hell. Are there any 'conventions' for implementing interruptible recursive functions, maybe a set of macros or something? I would like to use only standardized functions, no inline asm in order to stay portable.
Addition:
The prototype is here: https://github.com/open62541/open62541/compare/master...chunking_longjmp
The 'usage' is shown inside of the unit-test.
Currently, coroutine behavior is implemented for a non-recursive function Array_encodeBinary. However, the 'coroutine' behavior should be extended to the general recursive UA_encodeBinary function located here: https://github.com/open62541/open62541/blob/master/src/ua_types_encoding_binary.c#L1029
As pointed out by Olaf the easiest way would be to use an iterative algorithm. However, if for some reason this is difficult, you can always simulate the recursive algorithm with a stack container and a while loop. This at least makes the function easier to interrupt. Pretty good article of how to implement this can be found here. The article is written for c++, but it should not be difficult to convert it to c.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
A friend of mine needs to learn programming C in school, but unfortunately he has troubles with it and his teacher seems to be pretty incapable, for example, they are using Eclipse IDE and the teacher never showed how to do a Refactor => Rename, for example (so all pupils search and replace stuff manually when they need to).
I promised my friend to help him optimize (fortunately running) app which he copy&pasted with trial&error for hours now. But since I'm a Ruby programmer for years now and touched C/C++ only for a few weeks in a programmer's beginner course about 10 years ago myself, the world of non-OO C is totally unknown for me.
So I would really appreciate some good advice on how to refactor the following small app:
http://speedy.sh/Vnfnw/Energie.zip
It basically reads a CSV (energy/heat values?), computes some stuff, and then prepares some data which should later be sent to GNUplot.
For example, I'm unsure about this:
Is it good practice to send variables into a function as pointers and manipulate them within the function body? From and OOP, I'm used to be very careful with such things and try to change variables only by sending them to some method and assigning the method's return value to the variable again, e.g. x = sum(x, y) (or with ! methods).
I'm sure there is a lot more to optimize in the code. I'm thankful for some basic hints on how to optimize the code. It doesn't have to be perfect, but it's quite a mess at the moment, and before refactoring it into the "wrong" direction, it would be nice to get some feedback here.
Thank you.
"Is it good practice to send variables into a function as pointers and manipulate them within the function body?"
If you need to change them (pass by reference) there is no other way in C. It is a standard way. Also, there are no methods. Only functions. Although you can model OOP by structs and function pointers, for the small program it is not worth it.
You just must be much more attentive when programming in C as any wrong pointer can lead the whole application to crash.
Also I advise your friend to write program by himself and not refactor copy-paste code. From my point of view, the success of refactoring lays in understanding the program much more than on knowing where is "Refactor" menu item.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What are the key differences between Ruby and C?
They are almost totally different.
Ruby
Strong, dynamic typing
Purely object oriented
Automatic garbage collection and no pointers
Interpreted (or JIT compilation with JRuby/IronRuby)
Reflective
Supports functional programming (closures, coroutines, etc.)
No preprocessor or macros
C
Weak, static typing
Procedural (not object oriented)
Not garbage collected and has pointers
Compiled
No reflection
Does not support functional programming
Has a preprocessor and supports macros
To Ruby From C and C++
Why do you ask? Do you have a specific project or goals in mind?
In addition to what others have already mentioned; I'd also say that some key differences to keep in mind is that the C family is much more portable....or rather, much easier to distribute the finished software. C programs will also be much faster than Ruby...whether that is important or not depends on what you are building (well, that's ALWAYS important, but it isn't a make or break proposition for a lot of programs).
Ruby is just simply a beautiful language to work with (do not underestimate the importance of a language that works with you); developing programs is much quicker in Ruby than C ( C is a compiled language, so that is to be expected )...Ruby is also a pretty simple language to learn; most people consider C to be fairly tough for newbies to pick up.
-- edit --
wow, just saw this was a 3 year old thread....my bad